ASP.NET Excel的生成,读取,和2003与2007兼容

生成:

Excel表格的生成我只做了生成2003版本的,2007没有做因为很麻烦,还有用XML所以放弃了

  public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm control is rendered for
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=pdTable.xls");
        Response.Charset = "gb2312";
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }

因为我机子上装的是2007所以打开的时候总是提示但是每次打开却出现“您尝试打开的文件***的格式与文件扩展名指定的格式不一致,这个问题,但是并不影响显示,在2003下打开是没有问题。

 

读取:

也就是因为上面的问题,造成我读取的时候总是不认可该Excel,2003人为他的格式不正确,2007吧就更不用说了,后缀都不对。没有办法,只能带打开该Excel将其另存为97-2003兼容版本。

 

 private System.Data.DataTable GetDataFromExcelWithAppointSheetName(string Path)
    {

        //连接串
        //excel 2000 ~ 2003的 OleDb 连接串的格式如下:
        //Provider=Microsoft.Jet.OleDb.4.0;Data Source='excel文件路径';Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'
        //excel 2007 的 OleDb 的连接串的格式如下:
        //Provider=Microsoft.Ace.OleDb.12.0;Data Source='excel文件路径';Extended Properties='Excel 12.0;HDR=YES;IMEX=1;'
        try
        {
            string strConn = string.Empty;
            if (System.IO.Path.GetExtension(Path).Equals(".xlsx"))
                strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';data source=" + Path;
            else if (System.IO.Path.GetExtension(Path).Equals(".xls"))
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            //返回Excel的架构,包括各个sheet表的名称,类型,创建时间和修改时间等
            System.Data.DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });

            //包含excel中表名的字符串数组
            string[] strTableNames = new string[dtSheetName.Rows.Count];
            for (int k = 0; k < dtSheetName.Rows.Count; k++)
            {
                strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
            }

            OleDbDataAdapter myCommand = null;
            System.Data.DataTable dt = new System.Data.DataTable();

            //从指定的表明查询数据,可先把所有表明列出来供用户选择
            string strExcel = "select * from [" + strTableNames[0] + "]";
            myCommand = new OleDbDataAdapter(strExcel, strConn);
            dt = new System.Data.DataTable();
            myCommand.Fill(dt);
            conn.Close();
            conn.Dispose();

            this.GridView1.DataSource = dt;
            this.GridView1.DataBind();

            return dt;
        }
        catch
        {
            Label1.Text = "请更换Excel表格的数据格式,变成98-2003兼容版本";
            return null;
        }
    }

不知道其他人是怎么解决Excel的版本问题的,我只能暂时先这样解决了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值