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打开文件然后另存为,再处理另存为的文件试试
或者用记事本打开输出的excel文件看看是不是这个问题
如果不是这个原因,估计就是内码转换问题
看这吧~~
http://www.itstrike.cn/Home.mvc/Search?key=EXCEL
那还是因为保存方式问题吧
因为你的文件实际保存的是html格式的excel文件,所以无法识别
怎么把这个导出成excel,其他的解决方法嘛……
1、
一般是使用vs自带的office tools命名空间
使用他的打开和保存方法就不会出错用office tools的缺点是,需要装office,而且每次处理文件都会打开一个office程序实例而且可能占用比较大内存
优点是和使用office相同,可以实现很多功能,只要是office能识别的格式都能处理2、MSOWC,微软的 office web component组件也行3、保存成TXT或者XML来读取
不知道你的具体要求是什么,如果说只是简单的想保存,然后获取值,可以在读的时候,直接处理html试试
一般是使用vs自带的office tools命名空间
使用他的打开和保存方法就不会出错 用office tools的缺点是,需要装office,而且每次处理文件都会打开一个office程序实例而且可能占用比较大内存
优点是和使用office相同,可以实现很多功能,只要是office能识别的格式都能处理
这种方式怎么处理啊?能详细些吗?有例子或代码最好
建议搜索一下 例子还是很多的……office组件里有个application类,建议看下msdn
如果是网页操作,建议看office web component