C#调用Excel的API生成excel文件

C#调用Excel的API生成excel文件源代码,Winform代码测试没错,部署时,只要引用两个dll就可以了Microsoft.Office.Interop.Excel.dllOffice.dll

    public static void DataSetToLocalExcel(DataSet dataSet, string outputPath, bool deleteOldFile)  
           {  
               if (deleteOldFile)  
               {  
                   if (System.IO.File.Exists(outputPath)) { System.IO.File.Delete(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  
              foreach (System.Data.DataTable dt in dataSet.Tables)  
              {  
                  // 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);  
                  excelSheet.get_Range(excelRange, Type.Missing).Value2 = rawData;  
                  // Mark the first row as BOLD  
                  ((Range)excelSheet.Rows[1, Type.Missing]).Font.Bold = true;  
              }  
              //excelApp.Application.AlertBeforeOverwriting = false;  
              excelApp.Application.DisplayAlerts = false;  
              // 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();  
          }  

 

说明下,其中的   xlsApp.Application.DisplayAlerts   =   false;  的作用是 不显示确认对话框    

也可以逐Cell读取,那样可能会慢。本方法速度还过得去。

 

 

 

加支付宝好友偷能量挖...


评论(0)网络
阅读(98)喜欢(0)Asp.Net/C#/WCF