将datagrid导出为excel以及公司徽标,名称和地址 [英] exporting datagrid to excel along with the company logo, name and address
本文介绍了将datagrid导出为excel以及公司徽标,名称和地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在公司的商店管理上做一个Windows应用程序,我已经成功地将我的数据网格导出到excel但是我想知道是否有办法设置文件的标题类似于公司字母头的名称,徽标和地址。
我用来导出数据网格的代码是
Im doing a windows application on store management for a company, I have exported my datagrid successfully to excel but I would like to know if there is a way to set a header to the document similar to a company letter head with its name, logo and address.
The code I used to export my datagrid was
private void button2_Click_1(object sender, EventArgs e)
{
Excel.Application app = new Excel.Application();
app.Visible = true;
Excel.Workbook wb = app.Workbooks.Add(1);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
// changing the name of active sheet
ws.Name = "Exported from gridview";
ws.Rows.HorizontalAlignment = HorizontalAlignment.Center;
// storing header part in Excel
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
ws.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
ws.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
// sizing the columns
ws.Cells.EntireColumn.AutoFit();
// save the application
wb.SaveAs("D:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Exit from the application
app.Quit();
}
请帮助我,我应该做些什么修改来添加公司的详细信息在展示
Please help me with this, what modification should i do to add the company details in the header in the excel doc automatically while exporing
推荐答案
时自动在Excel文档中添加标题阅读此博客中的最后一个答案。对您的要求的非常好的解释
http://stackoverflow.com/questions/9410401/adding-custom-header-to-the-excel-file [ ^ ]
接受如果发现有帮助则投票
--RDBurmon
Read last answer in this blog . Very good explanation over your requirement
http://stackoverflow.com/questions/9410401/adding-custom-header-to-the-excel-file[^]
Accept and vote if found helpful
--RDBurmon
这篇关于将datagrid导出为excel以及公司徽标,名称和地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文