DataGridView到Excel与彩色单元格

时间:2014-10-02 10:31:00

标签: c# sql-server excel

我看了很多例子和演示,但我不能这样做。

我正在尝试将datagridview转换为excel。

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

但它转换为excel就像这样 http://i.imgur.com/0OXkUkL.png

我想复制单元格大小和颜色但是如何?

我使用此代码转换为excel

        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);

2 个答案:

答案 0 :(得分:2)

将内部循环更新为:

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#

答案 1 :(得分: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. */
}