使用savedialogbox保存Excel文件时出现问题。 [英] Problem in saving an excel file using savedialogbox.

查看:92
本文介绍了使用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");
       }

推荐答案

为什么不使用SaveAs [ ^ ]。



还使用调试器检查您是否给出了 sd 变量的有效路径和文件名。



你也可以连接 BeforeSave [ ^ ]查看保存操作之前发生了什么。
Why not use SaveAs[^] instead.

Also using debugger check that you have given a valid path and file name to sd variable.

You can also wire BeforeSave[^] to see what happens before the save operation.


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文件时出现问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆