简体   繁体   English

从SQL Server表数据创建Excel工作表

[英]Creating Excel sheet from SQL server table data

I'm dynamically creating an Excel sheet from data retrieved from SQL Server with a button click and a function. 我正在通过单击按钮和功能,根据从SQL Server检索到的数据动态创建Excel工作表。 Everything works fine at the moment as I have some 300 odd records but I am concerned about performance for more records. 目前,一切正常,因为我有300条奇数记录,但我担心更多记录的性能。

If I have some 10,000 records what modifications do I need to do to the code, so that I don't encounter any error while creating the page dynamically? 如果我有10,000条记录,我需要对代码进行哪些修改,以便在动态创建页面时不会遇到任何错误?

Below is the code I used : 以下是我使用的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(connectionString))
    {

        SqlCommand cmd = new SqlCommand("usp_myReport", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        Excel_FromDataTable(dt);
        Label1.Text = "Excel file created";
    }
}

Below is the function: 下面是功能:

private static void Excel_FromDataTable(DataTable dt)
{

    Excel.Application excel = new Excel.Application();
    Excel.Workbook workbook = excel.Application.Workbooks.Add(true);

    int iCol = 0;
    string[] colNames = new string[dt.Columns.Count];
    foreach (DataColumn c in dt.Columns)
    {
        colNames[iCol++] = c.ColumnName;
        char lastColumn = (char)(65 + dt.Columns.Count - 1);
        excel.get_Range("A1", lastColumn + "1").Value2 = colNames;
       }

    int iRow = 0;
    foreach (DataRow r in dt.Rows)
    {
        iRow++;
                    iCol = 0;
        foreach (DataColumn c in dt.Columns)
        {
            iCol++;
            excel.Cells[iRow + 1, iCol] = r[c.ColumnName];
        }
    }

    object missing = System.Reflection.Missing.Value;

    workbook.SaveAs("MyExcelWorkBook.xls",
        Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing,
        false, false, Excel.XlSaveAsAccessMode.xlNoChange,
        missing, missing, missing, missing, missing);
    excel.Visible = true;
    Excel.Worksheet worksheet = (Excel.Worksheet)excel.ActiveSheet;
    ((Excel._Worksheet)worksheet).Activate();
    ((Excel._Application)excel).Quit();

}

Try This 尝试这个

Get Your Data in DataTable and then use this function 在DataTable中获取您的数据,然后使用此功能

 public void ExportToExcel_AsXlsFile(DataTable dt, string file_name)
        {
            var grid = new GridView();
            grid.DataSource = dt;
            grid.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename='" + file_name + "'.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            grid.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM