[英]DataGridView To Excel With Colored Cells
I looked a lots of example and demo but I could not to it. 我看了很多例子和演示,但我做不到。
I'm trying to convert datagridview to excel. 我正在尝试将datagridview转换为excel。
My Results http://i.imgur.com/ujvGiXX.png 我的结果http://i.imgur.com/ujvGiXX.png
But its convert to excel like this http://i.imgur.com/0OXkUkL.png 但是它可以像这样转换为excel http://i.imgur.com/0OXkUkL.png
I want to copy cells size and color but how? 我要复制单元格的大小和颜色,但是如何复制?
I convert to excel with this code 我用这段代码转换为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);
update the internal loop to be: 将内部循环更新为:
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 );
}
}
don't forget: 不要忘记:
using Microsoft.Office.Interop.Excel;
see: 看到:
Change the background of Cells with C# 使用C#更改单元格的背景
Try this code for exporting to excel instead of what you are using. 尝试使用此代码导出到excel而不是使用的代码。 I use this for web apps though.
我将其用于网络应用程序。
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. */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.