在asp.net中将数据从数据库导出到excel

时间:2017-01-25 12:07:07

标签: c# mysql asp.net

使用c#从数据库中保存excel中的数据时出错。当点击按钮我只能获得我的Excel文件中的一个数据 welcome.xlsx 时,请建议我修复此错误。谢谢 System.Runtime.InteropServices.COMException(0x800A03EC):来自HRESULT的异常:0x800A03EC,位于F:\的BasicGridView.btnclick(Object sender,EventArgs e)的Microsoft.Office.Interop.Excel._Worksheet.set_Name(String RHS) shubham \ esin \ esin \ Default.aspx.cs:第73行

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class BasicGridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            string connectionString = "server=Myserver; database=northwind; user id=sasa; password=123;integrated security=false ";

            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("SELECT * FROM uregistration", con);

            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataTable dt = new DataTable();
            dt.TableName = "uregistration";
            sda.Fill(dt);

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

    }
    protected void btnclick(object sender, EventArgs e)
    {

        string connectionString = "server=Myserver; database=northwind; user id=sa; password=123;integrated security=false ";

        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("SELECT * FROM uregistration", con);

        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = cmd;
        DataTable dt = new DataTable();

        sda.Fill(dt);

        object misValue = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
        app.Visible = false;


        Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
        Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;

        for(int i =0;i<dt.Columns.Count;i++)
        {
            ws.Cells[1, i + 1] = dt.Columns[i].ColumnName;
        }

        for(int i=0; i < dt.Rows.Count; i++)
        {
            for(int j=0; j < dt.Columns.Count; j++)
            {
                ws.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();
            }
        }

        try {
            ws.Name = dt.TableName;
        }catch(Exception ex)
        {
            lbl.Text = ex.ToString();
        }
        wb.SaveAs("welcome2.xl", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,misValue,misValue,misValue);

        wb.Close(true, misValue, misValue);
        app.Quit();
    }
}

1 个答案:

答案 0 :(得分:0)

我只用一次修改测试了您的代码,它也可以显示excel表中的所有已获取数据,无异常

wb.SaveAs("welcome2.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,misValue,misValue,misValue);

扩展名&#34; .xl&#34;错了。 如果在具有相同名称且在同一目的地的文件已存在的情况下写入文件时发生异常,则出现对话框,要求您使用“是”,“否”,“取消”覆盖该文件,如果选择“取消”,则会抛出异常。您应检查已存在的文件,然后重命名/删除现有文件,然后编写新文件。 希望它有所帮助。