C# dataGridView导出EXCEL,保存前显示全部数据
C# dataGridView导出EXCEL,保存前显示全部数据 ,因为导出的EXCEL不需要保存的只作临时文件
原来在网上找到的代码点击按钮后导出EXCEL之前要填写文件名及路径, 但现在想要的是导出EXCEL后暂时不保存而显示EXCEL里面的所有内容项.
private void btnExcel_Click(object sender, EventArgs e)
{
string fileName = "Book1";
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
//取消
int r = 0;
if (saveFileName.IndexOf(":") < 0)
{
return;
}
//创建Excel对象
Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的电脑未安装Excel");
return;
}
Excel.Workbooks workbooks = xlApp.Workbooks;
//工作簿
Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
//Sheet
Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
//写入Excel第一行
Excel.Range Range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, 3]);//Range对象
Range.MergeCells = true;//合并单元格
worksheet.Cells[1, 1] = "销售订单展开表";//单元格内文本
&nb