当数据中有特殊字符时导出为ex​​cel(法语描述)

时间:2013-09-23 10:18:35

标签: export-to-excel

我使用Excel查询表将数据导出到excel文件。将有一个文本文件,其中包含许多行的数据,每个单元格用|分隔字符。我使用以下代码将数据转储到excel文件

         try
        {
            //Start Excel and get Application object.
            oXL = new Excel.Application();
            oXL.Visible = false;
            oXL.UserControl = false;
            //Get a new workbook.
            oWBook = (Excel._Workbook)(oXL.Workbooks.Open(strFilename, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false));
            oSheet = (Excel._Worksheet)oWBook.ActiveSheet;
            oSheet.Activate();

            //Create header row 2
            oXL.ReferenceStyle = Microsoft.Office.Interop.Excel.XlReferenceStyle.xlR1C1;
            Excel.Range oRange2 = oSheet.get_Range("A2", "A2");
            oRange2.FormulaR1C1 = HeaderLine1;

            //Create header row 2
            oRange2 = oSheet.get_Range("A3", "A3");
            oRange2.FormulaR1C1 = HeaderLine2;

            oRange2 = oSheet.get_Range("A" + StartRowNumber.ToString().Trim(), Missing.Value);
            var temp = oSheet.QueryTables.Add("TEXT;" + TextFileName, oRange2, Missing.Value);

            temp.Name = TextFileName.ToUpper().Replace(".TXT", "");
            temp.PreserveFormatting = true;
            temp.AdjustColumnWidth = false;
            temp.TextFileStartRow = 1;
            temp.TextFileParseType = Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited;
            temp.TextFileTextQualifier = Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierDoubleQuote;
            temp.TextFileConsecutiveDelimiter = false;
            temp.TextFileTabDelimiter = true;
            temp.TextFileSemicolonDelimiter = false;
            temp.TextFileCommaDelimiter = false;
            temp.TextFileSpaceDelimiter = false;
            temp.TextFileOtherDelimiter = "|";
            temp.TextFilePlatform = 1252;//65001;//1252;//65001;//1200;
            //http://en.wikipedia.org/wiki/Code_page
            temp.TextFileColumnDataTypes = GetDataType(ColumnDataType);
            temp.TextFileTrailingMinusNumbers = true;
            temp.Refresh(false);

            //temp.Connection.ToString();
            temp.Delete();



            oXL.ReferenceStyle = Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1;
            oWBook.Save();
            oWBook.Close(false, Missing.Value, Missing.Value);

        }
        catch (Exception ex)
        {
            oWBook.Close(false, Missing.Value, Missing.Value);
        }
        finally
        {
            oXL.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oSheet);
            oSheet = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oWBook);
            oWBook = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL);
            oXL = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

当文本文件包含一些特殊字符(Ex French words)时,在生成后打开excel文件时,它将不会显示。

任何人都可以帮忙解决这个问题吗?

由于 苏雷什

0 个答案:

没有答案