1)阅读此 excel 时。我收到“外部表不是预期格式”之类的错误。
2)当尝试通过双击打开一个excel文件时,我会变成这样
“您要打开的文件 ”just.xls' 的格式与文件扩展名指定的格式不同。在打开文件之前,请确认文件没有损坏并且来自受信任的来源。是否要打开现在的文件?"
请给出任何解决方案。这其中出了什么问题?请帮忙..!时间:2019-04-01 标签:c#asp.netexcel
我正在使用以下代码在 Excel 中转换 Gridview:-
string myfilename ="Attendenceformat" ;
string attachment = "attachment; filename=" + myfilename + ".xls";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter swriter = new StringWriter();
HtmlTextWriter htmlwriter = new HtmlTextWriter(swriter);
// Create a form to contain the gridview(MyGridView)
HtmlForm mynewform = new HtmlForm();
GridView1.Parent.Controls.Add(mynewform);
mynewform.Attributes["runat"] = "server";
mynewform.Controls.Add(GridView1);
mynewform.RenderControl(htmlwriter);
Response.Write(swriter.ToString());
Response.End();
上传生成的 Excel 的代码如下
if ((txtFilePath.HasFile))
{
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string query = null;
string connString = "";
string strFileName = txtFilePath.FileName;
//string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();
//Check file type
if (strFileType == ".xls" || strFileType == ".xlsx")
{
//print progressbar
txtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType));
}
else
{
lblMessage.Text = "Only Excel files(.xls or .xlsx) allowed";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
return;
}
string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType);
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
conn = new OleDbConnection(connString);
cmd.Connection = conn;
conn.Open();
dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
lblMessage.Text = "Upload Excel files";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
}
string SheetName = dt.Rows[0]["TABLE_NAME"].ToString();
query = "SELECT [Id],[RollNo],[Name],[Branch],[Sem],[Year],[Batch],[Subject],[Sessional],[Attendence],[OutOff] FROM [Attendenceformat(1)$]";
//Create the connection object
//Open connection
if (conn.State == ConnectionState.Closed) conn.Open();
//Create the command object
cmd = new OleDbCommand(query, conn);
// cmd.Parameters.Add("stid",null);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
grvExcelData.DataSource = ds.Tables[0];
grvExcelData.DataBind();
lblMessage.Text = "Data retrieved successfully! Total Recodes:" + ds.Tables[0].Rows.Count;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Visible = true;
da.Dispose();
conn.Close();
conn.Dispose();
}
else
{
lblMessage.Text = "Please select an excel file first";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
}
}
catch (Exception em)
{
lblMessage.Text = em.Message;
lblMessage.Visible = true;
}
}