C#与EXCEL文件导入导出的问题

tianhw1980 2009-04-20 03:36:26
我的导出部分:

<%@ 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上,读取数据就不会出现问题

这是怎么回事呢?

希望大家帮帮忙,解释一下!
...全文
109 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
supesa 2009-04-20
  • 打赏
  • 举报
回复
oda.Fill(ds); 这句报错吗 ?你 看你的xls文件里边里边是规范的列表吗?有没有合并单元格的情况
utopia54 2009-04-20
  • 打赏
  • 举报
回复
呵呵,帮顶...UP

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧