当前位置: 代码迷 >> C# >> c#写EXCEL,文件打开异常
  详细解决方案

c#写EXCEL,文件打开异常

热度:47   发布时间:2016-05-05 04:10:52.0
c#写EXCEL,文件打开错误!

public bool SaveDataTableToExcel(System.Data.DataTable excelTable, string filePath)
        {
            Microsoft.Office.Interop.Excel.Application app = new Application();

            try
            {
                app.Visible = false;

                Workbook wBook = app.Workbooks.Add(true);
                Worksheet wSheet = wBook.Worksheets[1];

                if (excelTable.Rows.Count > 0)
                {
                    int row = excelTable.Rows.Count;
                    int col = excelTable.Columns.Count;

                    for (int i = 0; i < row; i++)
                    {
                        for (int j = 0; j < col; j++)
                        {
                            string str = excelTable.Rows[i][j].ToString();
                            wSheet.Cells[i + 2, j + 1] = str;
                        }
                    }
                }

                for (int i = 0; i < excelTable.Columns.Count; i++)
                {
                    wSheet.Cells[1, 1 + i] = excelTable.Columns[i].ColumnName;
                }

                //屏蔽提示
                app.DisplayAlerts = false;
                app.AlertBeforeOverwriting = false;

                //保存工作簿 
                wBook.Save();
                
                //保存excel文件 
                app.Save(filePath);
                app.SaveWorkspace(filePath);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                app.Quit();
                app = null;
            }

代码也是网上找的
生成的EXCEL文件,打开提示
------解决思路----------------------
引用:

你的机器office安装的是2003以上版本
你生成excel的时候,存储的是2003支持的格式(xls)
------解决思路----------------------
   ofd.Filter = "Excel 工作簿 (*.xlsx)
------解决思路----------------------
*.xlsx
------解决思路----------------------
Excel 97-2003 工作簿 (*.xls)
------解决思路----------------------
*.xls";好像有个打开文件类型格式的

------解决思路----------------------
你导出的时候就不是excel格式文件,当然用代码打不开的

你直接双击文件的时候,是否提示你扩展名不一致,点了是之后才能打开?

不要用html方式导出
导出的时候要先导出到服务器本地,然后下载
------解决思路----------------------
如何创建Excel文件并保存
 private void btn_Create_Click(object sender, EventArgs e)
        {
            string P_str_path = txt_Path.Text;//记录路径
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//实例化Excel对象
            Microsoft.Office.Interop.Excel.Workbook newWorkBook = excel.Application.Workbooks.Add(true);//添加新工作簿
            object missing = System.Reflection.Missing.Value;//获取缺少的object类型值
            newWorkBook.Worksheets.Add(missing, missing, missing, missing);//向Excel文件中增加工作表
            if (P_str_path.EndsWith("\\"))//判断路径是否\结尾
                newWorkBook.SaveCopyAs(P_str_path + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");//保存Excel文件
            else
                newWorkBook.SaveCopyAs(P_str_path + "\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");//保存Excel文件
            MessageBox.Show("Excel文件创建成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);//弹出提示信息
            System.Diagnostics.Process[] excelProcess = System.Diagnostics.Process.GetProcessesByName("EXCEL");//实例化进程对象
            foreach (System.Diagnostics.Process p in excelProcess)//codego.net/
                p.Kill();//关闭进程
        }
  相关解决方案