2014-10-02 95 views
1

我看了很多的例子和演示,但我不能。DataGridView到彩色单元格的Excel

我想将datagridview转换为excel。

我的结果 http://i.imgur.com/ujvGiXX.png

但其转换到Excel这样 http://i.imgur.com/0OXkUkL.png

我要复制单元格的大小和颜色,但怎么样?

我转换与此代码

 Excel.Application xlApp; 
     Excel.Workbook xlWorkBook; 
     Excel.Worksheet xlWorkSheet; 
     object misValue = System.Reflection.Missing.Value; 

     Int16 i, j; 

     xlApp = new Excel.Application(); 
     xlWorkBook = xlApp.Workbooks.Add(misValue); 

     xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 

     for (i = 0; i <= dataGridView1.RowCount - 2; i++) 
     { 
      for (j = 0; j <= dataGridView1.ColumnCount - 1; j++) 
      { 
       xlWorkSheet.Cells[i + 1, j + 1] = dataGridView1[j, i].Value.ToString(); 
      } 
     } 

     SaveFileDialog sfd = new SaveFileDialog(); 
     sfd.Filter = "Excel Documents (*.xls)|*.xls"; 
     sfd.FileName = listBox1.SelectedItem.ToString() + " " + listBox3.SelectedItem.ToString() + " Stok Reçeteleri" + ".xls"; 


     if (sfd.ShowDialog() == DialogResult.OK) 
     { 
      //ToCsV(dataGridView1, sfd.FileName); // Here dataGridview1 is your grid view name 
      xlWorkBook.SaveAs(sfd.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
      //xlWorkBook.SaveAs(sfd.FileName, Excel.XlFileFormat.X 
      FileInfo fileInfo = new FileInfo(sfd.FileName); 
     } 


     xlWorkBook.Close(true, misValue, misValue); 
     xlApp.Quit(); 

     releaseObject(xlWorkSheet); 
     releaseObject(xlWorkBook); 
     releaseObject(xlApp); 
+0

你可能使用最好epplus HTTPS: //www.nuget.org/packages/EPPlus而不是办公室interop stu ff - 尤其是服务器端。 – 2014-10-02 10:49:49

+0

感谢您的建议。 – 2014-10-02 11:53:23

+0

是的,使用EPPlus代替Interop。 Interop应该只是最后的手段,即使这样你也应该重新思考。 – Ben 2014-10-02 13:34:12

回答

1

更新内部循环是:

for (i = 0; i <= dataGridView1.RowCount - 2; i++) 
      { 
       for (j = 0; j <= dataGridView1.ColumnCount - 1; j++) 
       { 
        Range range = (Range)xlWorkSheet.Cells[i + 1, j + 1]; 
        xlWorkSheet.Cells[i + 1, j + 1] = dataGridView1[j, i].Value.ToString(); 
        range.Interior.Color = System.Drawing.ColorTranslator.ToOle(dataGridView1.Rows[i].DefaultCellStyle.BackColor); 

       } 
      } 

不要忘记:

using Microsoft.Office.Interop.Excel; 

见:
Change the background of Cells with C#

+0

我只是改变这一行像这样 range.Interior.Color = System.Drawing.ColorTranslator.ToOle(dgv.Rows [i] .DefaultCellStyle.BackColor); – 2014-10-02 11:51:30

0

脱颖而出试试这个代码导出到Excel,而不是你使用的是什么。尽管如此,我仍然将它用于网络应用

private void ExportGridToExcel() 
{ 
    Response.Clear(); 
    Response.Buffer = true; 
    Response.ClearContent(); 
    Response.ClearHeaders(); 
    Response.Charset = ""; 
    string FileName = "SomeFileName" + DateTime.Now + ".xls"; 
    StringWriter strwritter = new StringWriter(); 
    HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter); 
    Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    Response.ContentType = "application/vnd.ms-excel"; 
    Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName); 
    GridView1.GridLines = GridLines.Both; 
    GridView1.HeaderStyle.Font.Bold = true; 
    GridView1.RenderControl(htmltextwrtter); 
    Response.Write(strwritter.ToString()); 
    Response.End(); 
} 
public override void VerifyRenderingInServerForm(Control control) 
{ 
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET 
     server control at run time. */ 
}