使用SaveWorkSpace保存Excel文件 [英] Save Excel file using SaveWorkSpace

查看:331
本文介绍了使用SaveWorkSpace保存Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我将值从Gridview导出到excel,我想通过显示另存为对话框来保存excel。但我收到一个错误说



Hi,

I am exporting values from Gridview to excel and I want to save the excel by displaying save as dialog box. But I am getting an error saying

'customer_details.xls' cannot be accessed. The file may be corrupted, location on a server that is not responsing, or read-only. <blockquote class="FQ"><div class="FQA">Quote:</div>





以下是我的代码:





Below is my code:

protected void btnExort_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
            app.Visible = false;
            Microsoft.Office.Interop.Excel.Workbook workbook = app.Workbooks.Add(1);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
            Microsoft.Office.Interop.Excel.Workbook objSheet = null;
            Microsoft.Office.Interop.Excel._Workbook objBook = null;
            for (int gridRow = 0; gridRow < gridView.Rows.Count; gridRow++)
            {
                GridViewRow row = gridView.Rows[gridRow];
                string name = ((Label)row.FindControl("Label1")).ToString();
                worksheet.Cells[gridRow + 3, 3] = ((Label)row.FindControl("Label1")).Text;
                worksheet.Cells[gridRow + 3, 2] = ((Label)row.FindControl("Label2")).Text;
                worksheet.Cells[gridRow + 3, 4] = ((Label)row.FindControl("Label3")).Text;
                worksheet.Cells[gridRow + 3, 5] = ((Label)row.FindControl("Label4")).Text;
                worksheet.Cells[gridRow + 3, 6] = ((Label)row.FindControl("Label7")).Text;
                worksheet.Cells[gridRow + 3, 7] = ((Label)row.FindControl("Label8")).Text;
            }

            worksheet.Cells[2, 2] = "Customer Name";
            worksheet.Cells[2, 3] = "Customer ID";
            worksheet.Cells[2, 4] = "Customer Display Name";
            worksheet.Cells[2, 5] = "Customer Email";
            worksheet.Cells[2, 6] = "Service ID";
            worksheet.Cells[2, 7] = "Service Name";
            //excel.Range cells = worksheet.Cells["B2", "D2"]; 
            DateTime currentTime;
            currentTime = DateTime.Now;

            string fileName = "customer_details.xls";
            //+currentTime;
            try
            {
                app.SaveWorkspace("D:\\ExcelWork\\ExportToExcel\\" + fileName);
                app.Workbooks.Close();

            }
            catch (Exception ex) { }
             
        }

推荐答案

可能正在访问的文件'customer_details.xls'已打开。请确认excel文件未打开而不是尝试。
May be the file "'customer_details.xls' you are accessing is open. Please confirm that excel file is not open and than try.


这篇关于使用SaveWorkSpace保存Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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