无法打开由 c# 创建的 excell 工作表

本文关键字:excell 工作 创建 | 更新日期: 2023-09-27 18:32:51

我在 c# 中创建简单的 excel 工作表,使用字符串作为输入链接创建了一个 excel 工作表。当我们打开保存的 excel 工作表时,它会显示类似 "excel found unreadable content in "sample.xls". Do you want to recover the contents of this workbook? If you trust the source of this workbook, Click Yes" 的消息框,它工作正常。我可以知道它是如何显示这样的吗?我的示例代码是

protected void Button1_Click(object sender, EventArgs e)
    {
        //Create the data set and table
        DataSet ds = new DataSet("New_DataSet");
        DataTable dt = new DataTable("New_DataTable");
        //Set the locale for each
        ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
        dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
        //Create a query and fill the data table with the data from the DB
         SqlConnection conn= new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
        SqlCommand cmd =new SqlCommand ("SELECT Name,EmpCode FROM EmpDetails where EmpCode='"+Neena102+"'",conn);
       // dr = conn.query(str);
        SqlDataAdapter adr = new SqlDataAdapter();
        adr.SelectCommand = cmd;
        adr.Fill(dt);
       //Add the table to the data set
        ds.Tables.Add(dt);
        var rows = ds.Tables[0].Rows;
        foreach (DataRow row in rows)
        {
            string name=Convert.ToString(row["Name"]);
        //Here's the easy part. Create the Excel worksheet from the data set
        ExcelLibrary.DataSetHelper.CreateWorkbook(@"F:''reports''"+name+".xls", ds);
        System.Diagnostics.Process.Start(@"F:''reports''" + name + ".xls");
        }
    }

无法打开由 c# 创建的 excell 工作表

请尝试

以下更改。

进程启动Excel = System.Diagnostics.Process.Start(@"F:''reports''" + name + ".xls");startExcel.WaitForExit();