datatable导出excel cell内容包含中英文时,出错。
使用如下代码。static class FastExportingMethod
    {
        public static void ExportToExcel(System.Data.DataTable dt, string outputPath)
        {
            // Create the Excel Application object
            ApplicationClass excelApp = new ApplicationClass();
            // Create a new Excel Workbook
            Workbook excelWorkbook = excelApp.Workbooks.Add(Type.Missing);
            int sheetIndex = 0;
            // Copy each DataTable            // Copy the DataTable to an object array
            object[,] rawData = new object[dt.Rows.Count + 1, dt.Columns.Count];            // Copy the column names to the first row of the object array
            for (int col = 0; col < dt.Columns.Count; col++)
            {
                rawData[0, col] = dt.Columns[col].ColumnName;
            }            //// Copy the values to the object array
            for (int col = 0; col < dt.Columns.Count; col++)
            {
                for (int row = 0; row < dt.Rows.Count; row++)
                {
                    rawData[row + 1, col] = dt.Rows[row].ItemArray[col];
                }
            }            // Calculate the final column letter
            string finalColLetter = string.Empty;
            string colCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            int colCharsetLen = colCharset.Length;            if (dt.Columns.Count > colCharsetLen)
            {
                finalColLetter = colCharset.Substring(
                    (dt.Columns.Count - 1) / colCharsetLen - 1, 1);
            }            finalColLetter += colCharset.Substring(
                    (dt.Columns.Count - 1) % colCharsetLen, 1);            // Create a new Sheet
            Worksheet excelSheet = (Worksheet)excelWorkbook.Sheets.Add(
                excelWorkbook.Sheets.get_Item(++sheetIndex),
                Type.Missing, 1, XlSheetType.xlWorksheet);            excelSheet.Name = dt.TableName;            // Fast data export to Excel
            string excelRange = string.Format("A1:{0}{1}",
                finalColLetter, dt.Rows.Count + 1);
            try
            {
                excelSheet.get_Range(excelRange, Type.Missing).Value2 = rawData;
///////////此处,内容包含中英文就出错怎么解决一下//////////////??????????????????
            }
            catch (System.Exception xxxx)
            {
                string x = xxxx.ToString();
            }            // Mark the first row as BOLD
            ((Range)excelSheet.Rows[1, Type.Missing]).Font.Bold = true;
            // Save and Close the Workbook
            excelWorkbook.SaveAs(outputPath, XlFileFormat.xlWorkbookNormal, Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            excelWorkbook.Close(true, Type.Missing, Type.Missing);
            excelWorkbook = null;            // Release the Application object
            excelApp.Quit();
            excelApp = null;            // Collect the unreferenced objects
            GC.Collect();
            GC.WaitForPendingFinalizers();        }
    }

解决方案 »

  1.   

    编码方式不对吧  留个邮箱 我可以发一个操作excel的类发给你 
      

  2.   

    DataTable完美导出EXECL        #region 打印与导出Excel表
            /// <summary>
            /// 名称:WriteToExcel
            /// 功能:查询结果导出Execl
            /// 编写人:
            /// 编写时间:2008-11-26
            /// </summary>
            /// <param name="table"></param>
            public void WriteToExcel(DataTable table)
            {
                try
                {
                    string tempImagePath = Application.StartupPath;
                    string temp = tempImagePath + "\\ExeclFiles";
                    Directory.CreateDirectory(@temp);
                    string strFilePath = @Application.StartupPath + @"\ExeclFiles\" + CommonClass.CommonDateTime() + ".xls";
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath,true, System.Text.Encoding.Default);
                    object[] values = new object[table.Columns.Count];
                    for (int i = 0; i < table.Columns.Count; ++i)
                    {
                        sw.Write(table.Columns[i].Caption.ToString());
                        sw.Write('\t');
                    }
                    sw.Write("\r\n");
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        for (int j = 0; j < values.Length; ++j)
                        {
                            sw.Write(table.Rows[i][j].ToString());
                            sw.Write('\t');
                        }
                        sw.Write("\r\n");
                    }
                    sw.Flush();
                    sw.Close();
                    CommonClass.MessageBoxOK("成功导出[" + ds.Tables[0].Rows.Count.ToString() + "]行到Execl!");
                }
                catch
                {
                    CommonClass.MessageBoxNo("导出Execl失败!");
                }
            }