2014-04-08 33 views
0

我需要将DataGridView的内容保存到Excel中,并且能够将DataGridView的单元格中的单元格颜色与Excel中的单元格进行匹配。C#Winforms格式保存为excel

如果我保存到CSV文件的保存是即时的,而是可以应用于任何风格,所以我需要使用Microsoft.Office.Interop.Excel

这给了我正确的造型却是那么那么那么那么慢。

有没有解决方法呢?

public static void ExportToExcel(this DataGridView Tbl, string ExcelFilePath = null) 
    { 
     try 
     { 
      if (Tbl == null || Tbl.Columns.Count == 0) 
       throw new Exception("ExportToExcel: Null or empty input table!\n"); 

      // load excel, and create a new workbook 

      Excel.Application excelApp = new Excel.Application(); 
      excelApp.Workbooks.Add(); 

      // single worksheet 
      Excel._Worksheet workSheet = excelApp.ActiveSheet; 

      // column headings 
      for (int i = 0; i < Tbl.Columns.Count; i++) 
      { 
       workSheet.Cells[1, (i + 1)] = Tbl.Columns[i].Name; 
      } 

      // rows 
      for (int i = 0; i < Tbl.Rows.Count; i++) 
      { 
       // to do: format datetime values before printing 
       for (int j = 0; j < Tbl.Columns.Count; j++) 
       { 
        workSheet.Cells[(i + 2), (j + 1)] = Tbl.Rows[i].Cells[j].Value; 
        ((Excel.Range)(workSheet.Cells[(i + 2), (j + 1)])).Interior.Color = System.Drawing.Color.Orange;//System.Drawing.ColorTranslator.ToOle(Tbl.Rows[i].Cells[j].Style.BackColor); 
        //workSheet.Cells[(i + 2), (j + 1)].Interior.Color = Tbl.Rows[i].Cells[j].Style.BackColor; 
       } 
      } 

      // check filepath 
      if (ExcelFilePath != null && ExcelFilePath != "") 
      { 
       try 
       { 
        workSheet.SaveAs(ExcelFilePath); 
        excelApp.Quit(); 
        MessageBox.Show("Excel file saved!"); 
       } 
       catch (Exception ex) 
       { 
        throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n" 
         + ex.Message); 
       } 
      } 
      else // no filepath is given 
      { 
       excelApp.Visible = true; 
      } 
     } 
     catch (Exception ex) 
     { 
      throw new Exception("ExportToExcel: \n" + ex.Message); 
     } 
    } 
+0

http://stackoverflow.com/questions/3989122/microsoft-office-interop-excel-really-slow看到 –

+1

你可以如EPPlus尝试使用第三方库(Excel中2007+)或Aspose。或者尝试分析你的Interop代码来理解为什么它“如此如此之慢”。可能有些事情可以改善表现,例如请参阅http://stackoverflow.com/questions/3989122/microsoft-office-interop-excel-really-slow/3989452#3989452 – Joe

+0

由于互操作性,此方法相当慢。您可以使用第三方工具,例如Flexcel Spreadsheet或Gembox Spreadsheet,也提供样式和格式。 –

回答

1

你可以使用这个库(opensource)。 代码:

//create new xls file 
string file = "C:\\newdoc.xls"; 
Workbook workbook = new Workbook(); 
Worksheet worksheet = new Worksheet("First Sheet"); 
worksheet.Cells[0, 1] = new Cell((short)1); 
worksheet.Cells[2, 0] = new Cell(9999999); 
worksheet.Cells[3, 3] = new Cell((decimal)3.45); 
worksheet.Cells[2, 2] = new Cell("Text string"); 
worksheet.Cells[2, 4] = new Cell("Second string"); 
worksheet.Cells[4, 0] = new Cell(32764.5, "#,##0.00"); 
worksheet.Cells[5, 1] = new Cell(DateTime.Now, @"YYYY\-MM\-DD"); 
worksheet.Cells.ColumnWidth[0, 1] = 3000; 
workbook.Worksheets.Add(worksheet); 
workbook.Save(file); 
+0

对不起,我忘了链接: http://epplus.codeplex.com/ – mister