C#与EXCEL文件导入导出,报错,怎么解决,大家帮忙啊!
我的导出部分:<%@ Page Language="C#" EnableEventValidation="false"protected void Button4_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
} public override void VerifyRenderingInServerForm(Control control)
{ }
在导出为EXCEL文件后,在另一个WEB中,我想获取我导出的EXCEL文件的某行的值,代码如下:在这之前,我将EXCEL文件的Sheet页改为了Sheet1
protected void Button3_Click(object sender, EventArgs e)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName(这里是文件的名称) + ";Extended Properties=Excel 8.0";
//链接Excel
OleDbConnection cnnxls = new OleDbConnection(strConn);
//读取Excel里面有 表Sheet1
OleDbDataAdapter oda = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls);
DataSet ds = new DataSet();
//将Excel里面有表内容装载到内存表中!
oda.Fill(ds);
cnnxls.Close();
//显示数据
this.GridView2.DataSource = ds;
this.GridView2.DataBind();
if (ds.Tables[0].Rows.Count > 0)
{
string connstr = "server=(local);database=db_people; uid=sa; pwd=sa;";
SqlConnection conn = new SqlConnection(connstr);
int count = ds.Tables[0].Rows.Count;
if (count>0)
{ this.DropDownList1.Text = ds.Tables[0].Rows[0][1].ToString();
this.TextBox2.Text = ds.Tables[0].Rows[0][2].ToString();
this.TextBox3.Text = ds.Tables[0].Rows[0][3].ToString();
this.TextBox4.Text = ds.Tables[0].Rows[0][4].ToString();
this.TextBox5.Text = ds.Tables[0].Rows[0][5].ToString();
this.DropDownList2.Text = ds.Tables[0].Rows[0][6].ToString();
this.TextBox7.Text = ds.Tables[0].Rows[0][7].ToString();
this.TextBox8.Text = ds.Tables[0].Rows[0][8].ToString();
this.TextBox9.Text = ds.Tables[0].Rows[0][9].ToString();
this.TextBox10.Text = ds.Tables[0].Rows[0][10].ToString();
this.TextBox11.Text = ds.Tables[0].Rows[0][11].ToString();
this.TextBox12.Text = ds.Tables[0].Rows[0][12].ToString();
this.TextBox13.Text = ds.Tables[0].Rows[0][13].ToString();
}
Response.Write("<script language=javascript>alert('数据获取成功!);</script>");
conn.Close();
} } 但是在 oda.Fill(ds);总是报错,说是 {"外部表不是预期的格式。"}
但是,我将原有的导出数据复制粘贴到新建EXCEL文件的Sheet1上,读取数据就不会出现问题这是怎么回事呢?希望大家帮帮忙,解释一下!
我的导出部分:<%@ Page Language="C#" EnableEventValidation="false"protected void Button4_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
} public override void VerifyRenderingInServerForm(Control control)
{ }
在导出为EXCEL文件后,在另一个WEB中,我想获取我导出的EXCEL文件的某行的值,代码如下:在这之前,我将EXCEL文件的Sheet页改为了Sheet1
protected void Button3_Click(object sender, EventArgs e)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName(这里是文件的名称) + ";Extended Properties=Excel 8.0";
//链接Excel
OleDbConnection cnnxls = new OleDbConnection(strConn);
//读取Excel里面有 表Sheet1
OleDbDataAdapter oda = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls);
DataSet ds = new DataSet();
//将Excel里面有表内容装载到内存表中!
oda.Fill(ds);
cnnxls.Close();
//显示数据
this.GridView2.DataSource = ds;
this.GridView2.DataBind();
if (ds.Tables[0].Rows.Count > 0)
{
string connstr = "server=(local);database=db_people; uid=sa; pwd=sa;";
SqlConnection conn = new SqlConnection(connstr);
int count = ds.Tables[0].Rows.Count;
if (count>0)
{ this.DropDownList1.Text = ds.Tables[0].Rows[0][1].ToString();
this.TextBox2.Text = ds.Tables[0].Rows[0][2].ToString();
this.TextBox3.Text = ds.Tables[0].Rows[0][3].ToString();
this.TextBox4.Text = ds.Tables[0].Rows[0][4].ToString();
this.TextBox5.Text = ds.Tables[0].Rows[0][5].ToString();
this.DropDownList2.Text = ds.Tables[0].Rows[0][6].ToString();
this.TextBox7.Text = ds.Tables[0].Rows[0][7].ToString();
this.TextBox8.Text = ds.Tables[0].Rows[0][8].ToString();
this.TextBox9.Text = ds.Tables[0].Rows[0][9].ToString();
this.TextBox10.Text = ds.Tables[0].Rows[0][10].ToString();
this.TextBox11.Text = ds.Tables[0].Rows[0][11].ToString();
this.TextBox12.Text = ds.Tables[0].Rows[0][12].ToString();
this.TextBox13.Text = ds.Tables[0].Rows[0][13].ToString();
}
Response.Write("<script language=javascript>alert('数据获取成功!);</script>");
conn.Close();
} } 但是在 oda.Fill(ds);总是报错,说是 {"外部表不是预期的格式。"}
但是,我将原有的导出数据复制粘贴到新建EXCEL文件的Sheet1上,读取数据就不会出现问题这是怎么回事呢?希望大家帮帮忙,解释一下!
直接操作EXCEL生成数据
参考