当前位置:编程学习 > C#/ASP.NET >>

导出excel 保存在桌面时出错

Microsoft.Office.Interop.Excel.Application applExcel=new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook xlWorkbook;
Microsoft.Office.Interop.Excel.Worksheet xlWorksheet;
System.Reflection.Missing oMissing = System.Reflection.Missing.Value;

try
{
applExcel.Visible=true;

xlWorkbook=applExcel.Workbooks.Add(oMissing);
xlWorksheet=(Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Worksheets.Add(oMissing,oMissing,1,oMissing);

xlWorksheet.Name = "NewWorksheet";
xlWorksheet.Cells[1, 1] = "Topic: ";
xlWorksheet.Cells[1, 2] = ".Net Interop Excel Demo";

xlWorkbook.SaveAs(saveAsPath, oMissing, oMissing, oMissing, oMissing,
oMissing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, oMissing, oMissing, oMissing, 
oMissing, oMissing);

applExcel.Quit();
}
catch(System.Exception ex)
{
//showError(ex.Message);
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(applExcel);
xlWorksheet=null;
xlWorkbook=null;
applExcel = null;
GC.Collect();
}

保存于其他盘都可以,唯独保存于桌面时会出错??? --------------------编程问答-------------------- 这种问题好象是因为桌面的文件夹路径中存在空格(比如Documents and Settings中间就有空格,可能把这个空格用其他字符替换了),所以导致。
我怀疑这个应该是个Bug。 --------------------编程问答--------------------        參考:
            Excel.Application appExcel = null;
            Excel._Workbook workbook = null;
            Excel._Worksheet worksheetGrid = null;
            try
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.InitialDirectory = "D:\\";
                dlg.Filter = "Excel 文件|*.xls";
                dlg.DefaultExt = "xls";
                dlg.AddExtension = true;
                dlg.OverwritePrompt = false;
                dlg.Title = "導出Excel文件,請指定路徑(操作員在此路徑需要有寫入權限), 同時輸入文件名";
                string fileName = string.Empty;
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    fileName = dlg.FileName;
                    if (fileName != string.Empty)
                    {
                        //狀態信息
                        this.Cursor = Cursors.WaitCursor;

                        appExcel = new Excel.ApplicationClass();

                        workbook = appExcel.Workbooks.Add(Type.Missing);

                
                        worksheetGrid.Activate();
                        appExcel.ActiveWindow.Zoom = 75;


                        //刪除多余的sheet  
                        Excel.Worksheet worksheet2 = (Excel.Worksheet)workbook.Worksheets.get_Item(2);
                        Excel.Worksheet worksheet3 = (Excel.Worksheet)workbook.Worksheets.get_Item(3);
                        worksheet2.Delete();
                        worksheet3.Delete();  

                        //處理數據進入Excel
                           --this.ExportData(worksheetGrid, mView);

                        appExcel.ActiveWorkbook.SaveAs(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive,
                                                       Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                        MessageBox.Show("導出成功,文件位于: " + fileName + "");

                        worksheetGrid = null;
                        workbook = null;
                        appExcel.Quit();
                        appExcel = null;
                    }
                }
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,