Пример #1
0
    public static void TranslateCsvToXls(string filePath, string outFolder)
    {
        FileInfo file = new FileInfo(filePath);

        if (file.Extension != ".csv")
        {
            return;
        }
        string name = file.Name.Substring(0, file.Name.LastIndexOf('.'));

        FileInfo fileCopy = new FileInfo(filePath.Replace(".csv", ".xml"));

        using (StreamReader sr = new StreamReader(filePath, Encoding.UTF8, false))
        {
            using (StreamWriter sw = new StreamWriter(fileCopy.FullName, false, Encoding.Unicode))
            {
                sw.Write(sr.ReadToEnd());
            }
        }

        //载入csv文档
        Workbook workbook = new Workbook();

        workbook.LoadFromFile(fileCopy.FullName, ",", 1, 1);
        string outFile = outFolder + "\\" + name + ".xls";

        //保存为xls格式
        workbook.SaveToFile(outFile, ExcelVersion.Version97to2003);
        fileCopy.Delete();

        JCExcel excecl = new JCExcel(outFile);

        excecl.ToExcelTwo(excecl.dataSet, outFile, outFile);
        // excecl.DataSetToExcel(excecl.dataSet, outFile);
        if (!excecl.CheckColumnContent())
        {
            MessageBox.Show(string.Format("{0}失败", name));
            File.Delete(outFile);
        }
        if (!excecl.DetectionOfDigits())
        {
            MessageBox.Show(string.Format("{0}失败", name));
            File.Delete(outFile);
        }
    }