本文介绍了将datagridview的行导出到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的应用程序中使用了datagridview,并用数据集填充它,该数据集返回两列ID和Name,但是在gridview中,我只显示name列,而隐藏ID列.是否可以将行单独从datagridview导出到excel.例如,如果遵循.名称显示在每一行中,
A
B
C
D
我的Excel应该采用每行都必须显示为列的格式
A B C D
我尝试导出的代码
I am using a datagridview in my application and I have filled it with dataset which returns two columns ID and Name, but in gridview I''m displaying the name column alone hiding the ID column. Is it possible to export the rows alone from datagridview to excel. Eg If the follow. names appear as each row,
A
B
C
D
My excel should be in a format that each row must display as a column
A B C D
Code Which I have tried for export
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Excel files |*.xls|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.CreatePrompt = true;
saveFileDialog1.Title = "Export Excel File To";
//saveFileDialog1.ShowDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "Exported from Data Template";
for (int i = 0; i < dgDtParameters.Rows.Count - 1; i++)
{
for (int j = 0; j < dgDtParameters.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dgDtParameters.Rows[i].Cells[j].Value.ToString();
}
}
fileName = saveFileDialog1.FileName;
workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
MessageBox.Show("Data template exported successfully","Data Template Export);
}
else
return;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
app.Quit();
workbook = null;
app = null;
GC.Collect();
}
推荐答案
这篇关于将datagridview的行导出到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!