C# 导出到Excel保持文件打开,而不导出第一个datagrid
我正在尝试将2个DataGridView导出到excel,但这两组数据网格视图都缺少最后一行数据,我已检查了代码,看不出我做错了什么?此外,它还在导出时创建临时文件,然后锁定该文件,而不是正常退出,只有重新启动才能删除该文件?任何帮助都将是伟大的代码如下。例如,如果我另存为test.xlsx,我会得到两个文件~$test.xlsx和test.xlsx,这两个文件都被锁定C# 导出到Excel保持文件打开,而不导出第一个datagrid,c#,excel,datagridview,C#,Excel,Datagridview,我正在尝试将2个DataGridView导出到excel,但这两组数据网格视图都缺少最后一行数据,我已检查了代码,看不出我做错了什么?此外,它还在导出时创建临时文件,然后锁定该文件,而不是正常退出,只有重新启动才能删除该文件?任何帮助都将是伟大的代码如下。例如,如果我另存为test.xlsx,我会得到两个文件~$test.xlsx和test.xlsx,这两个文件都被锁定 private void exprtbtn_Click(object sender, EventArgs e) {
private void exprtbtn_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
try
{
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet1 = null;
Microsoft.Office.Interop.Excel._Worksheet worksheet2 = null;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet1 = workbook.Sheets["Sheet1"];
worksheet1 = workbook.ActiveSheet;
// changing the name of active sheet
worksheet1.Name = "Switch Totals";
// storing header part in Excel
for (int i = 1; i < switchtotalgrd.Columns.Count + 1; i++)
{
worksheet1.Cells[1, i] = switchtotalgrd.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < switchtotalgrd.Rows.Count - 1; i++)
{
for (int j = 0; j < switchtotalgrd.Columns.Count; j++)
{
worksheet1.Cells[i + 2, j + 1] = switchtotalgrd.Rows[i].Cells[j].Value.ToString();
}
}
// Adding second worksheet
int count = workbook.Worksheets.Count;
Excel.Worksheet addedSheet = workbook.Worksheets.Add(Type.Missing,
workbook.Worksheets[count], Type.Missing, Type.Missing);
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet2 = workbook.Sheets["Sheet2"];
worksheet2 = workbook.ActiveSheet;
// changing the name of active sheet
worksheet2.Name = "Itemised Extn";
// storing header part in Excel
for (int i = 1; i < fullresult.Columns.Count + 1; i++)
{
worksheet2.Cells[1, i] = fullresult.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < fullresult.Rows.Count - 1; i++)
{
for (int j = 0; j < fullresult.Columns.Count; j++)
{
if (fullresult.Rows[i].Cells[j].Value == null)
{
fullresult.Rows[i].Cells[j].Value = "NA";
}
worksheet2.Cells[i + 2, j + 1] = fullresult.Rows[i].Cells[j].Value.ToString();
}
}
// save the application
string fileName = String.Empty;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Excel files |*.xls|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
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);
}
else
return;
}
//Catch all errors.
catch (System.Exception)
{
}
finally
{
workbook = null;
app = null;
}
}
private void exprtbtn\u单击(对象发送方,事件参数e)
{
Microsoft.Office.Interop.Excel.\u应用程序app=新的Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.\u工作簿=app.Workbooks.Add(Type.Missing);
尝试
{
//在工作簿中创建新的Excel工作表
Microsoft.Office.Interop.Excel.\u工作表1=null;
Microsoft.Office.Interop.Excel.\u工作表工作表2=null;
//获取第一张图纸的引用。默认情况下,其名称为Sheet1。
//将其引用存储到工作表
工作表1=工作簿.工作表[“工作表1”];
worksheet1=工作簿.ActiveSheet;
//更改活动图纸的名称
工作表1.Name=“开关总计”;
//在Excel中存储标题部分
对于(int i=1;i
您可能需要再次检查外部for循环,例如,for(int i=0;i
,它是否应该使用小于等于
由于您使用的是Excel com互操作,因此需要调用此方法才能在使用完资源后释放该资源
一个好的做法是使用try{…}catch{…}finally{…},并在finally块中释放com对象 谢谢你,是的,设置为