本文介绍了使用savedialogbox保存Excel文件时出现问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已将gridview值导出到excel文件。但是当我尝试使用保存对话框进行保存时,它不起作用,但是没有错误显示。请纠正我的错误。我正在使用的代码如下。
Hi,
I have exported the gridview value to an excel file.But when i tried it to save using a save dialog box,it's not working,but no error is showing.Kindly rectify my mistake.The code i am using is given below.
private void button2_Click(object sender, EventArgs e)
{
string sd;
saveFileDialog1.ShowDialog();
saveFileDialog1.InitialDirectory = "c:";
saveFileDialog1.FileName = "";
saveFileDialog1.Filter = "Excel File|*.xls|All Files|*.*";
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
ExcelApp.Application.Workbooks.Add(Type.Missing);
ExcelApp.Columns.ColumnWidth = 20;
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
ExcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
ExcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
sd = saveFileDialog1.FileName;
ExcelApp.ActiveWorkbook.SaveCopyAs(sd);
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
MessageBox.Show("Excel file created");
}
推荐答案
private void button2_Click(object sender, EventArgs e)
{
string sd;
saveFileDialog1.ShowDialog();
saveFileDialog1.InitialDirectory = "c:";
saveFileDialog1.Filter = "Excel File|*.xls|All Files|*.*";
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
ExcelApp.Application.Workbooks.Add(Type.Missing);
ExcelApp.Columns.ColumnWidth = 20;
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
ExcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
ExcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
sd = saveFileDialog1.FileName;
ExcelApp.ActiveWorkbook.SaveCopyAs(sd+".xls");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
MessageBox.Show("Excel file created");
}
这篇关于使用savedialogbox保存Excel文件时出现问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!