C# 无法打开由c创建的Excel工作表
我在link的帮助下创建了一个excel表格。当我们打开保存的excel工作表时,它会显示类似“excel在sample.xls中发现无法读取的内容”这样的消息框。是否要恢复此工作簿的内容?如果您信任此工作簿的来源,请单击“是”。我可以知道它是怎么表现出来的吗?我的示例代码是C# 无法打开由c创建的Excel工作表,c#,excel,excellibrary,C#,Excel,Excellibrary,我在link的帮助下创建了一个excel表格。当我们打开保存的excel工作表时,它会显示类似“excel在sample.xls中发现无法读取的内容”这样的消息框。是否要恢复此工作簿的内容?如果您信任此工作簿的来源,请单击“是”。我可以知道它是怎么表现出来的吗?我的示例代码是 protected void Button1_Click(object sender, EventArgs e) { //Create the data set and table
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");
}
}
请尝试以下更改 Process startExcel=System.Diagnostics.Process。Start@F:\reports\+name+.xls;
startExcel.WaitForExit 您应该使用SQL参数来防止SQL注入。@Sybren,如果使用SQL参数,我可以解决这个问题吗?否,但它是防止SQL注入的重要辅助节点。通常在Excel中打开文件时会收到相同的消息-您的问题在ExcelLibrary.DataSetHelper.CreateWorkbook中。数据集中有多少行?我曾经遇到过一个类似的问题,通过添加几行空行解决了这个问题,因此文件大小超过了一些限制。