如何在窗口应用程序C#中从gridview导出记录 [英] How to export records from gridview in window application C#
本文介绍了如何在窗口应用程序C#中从gridview导出记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在窗口应用程序中从Gridview导出记录c#
在Excel文件中记录不显示...
private void btn_Export_Click(object sender,EventArgs e)
{
Microsoft.Office.Interop.Excel ._Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
尝试
{
worksheet = workbook.ActiveSheet;
worksheet.Name =ExportedFromDatGrid;
int cellRowIndex = 1;
int cellColumnIndex = 1;
//遍历每一行并从每列读取值。
for(int i = 0; i< dataGridView.Rows.Count - 1; i ++)
{
for(int j = 0; j< dataGridView.Columns.Count; j ++ )
{
// Excel索引从1,1开始。由于第一行将具有列标题,因此添加条件检查。
if(cellRowIndex == 1)
{
worksheet.Cells [cellRowIndex,cellColumnIndex] = dataGridView.Columns [j] .HeaderText;
}
else
{
worksheet.Cells [cellRowIndex,cellColumnIndex] = dataGridView.Rows [i] .Cells [j] .Value.ToString();
}
cellColumnIndex ++;
}
cellColumnIndex = 1;
cellRowIndex ++;
}
//获取要从用户保存的Excel的位置和文件名。
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter =Excel文件(* .xlsx)| * .xlsx |所有文件(*。*)| *。*;
saveDialog.FilterIndex = 2;
if(saveDialog.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
workbook.SaveAs(saveDialog.FileName);
MessageBox.Show(导出成功);
}
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message);
}
最后
{
excel.Quit();
workbook = null;
excel = null;
}
}
我尝试过:
< pre>
如何在窗口应用程序中从Gridview导出记录c#
在Excel文件中记录不显示...
解决方案
< blockquote>参见这篇文章:将DataGridview导出到Excel [ ^ ]
How to Export Records from Gridview in window application c# Records Don'tShow in Excel File...
private void btn_Export_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing); Microsoft.Office.Interop.Excel._Worksheet worksheet = null; try { worksheet = workbook.ActiveSheet; worksheet.Name = "ExportedFromDatGrid"; int cellRowIndex = 1; int cellColumnIndex = 1; //Loop through each row and read value from each column. for (int i = 0; i < dataGridView.Rows.Count - 1; i++) { for (int j = 0; j < dataGridView.Columns.Count; j++) { // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check. if (cellRowIndex == 1) { worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView.Columns[j].HeaderText; } else { worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView.Rows[i].Cells[j].Value.ToString(); } cellColumnIndex++; } cellColumnIndex = 1; cellRowIndex++; } //Getting the location and file name of the excel to save from user. SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; saveDialog.FilterIndex = 2; if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { workbook.SaveAs(saveDialog.FileName); MessageBox.Show("Export Successful"); } } catch (System.Exception ex) { MessageBox.Show(ex.Message); } finally { excel.Quit(); workbook = null; excel = null; } } What I have tried: <pre> How to Export Records from Gridview in window application c# Records Don'tShow in Excel File...
解决方案
See this article: Exporting DataGridview To Excel[^]
这篇关于如何在窗口应用程序C#中从gridview导出记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文