Send query results to Excel from ASP.NET website

后端 未结 7 1062
失恋的感觉
失恋的感觉 2020-12-17 03:03

We let users create ad-hoc queries in our website. We would like to have the user select their criteria, then click submit and have the results streamed automatically to Ex

相关标签:
7条回答
  • 2020-12-17 03:12

    Once you have your Dataset you can convert it to an object[,] and insert it into an Excel document. Then you can save the document to disk and stream it to the user.

            //write the column headers
            for (int cIndex = 1; cIndex < 1 + columns; cIndex++)
                sheet.Cells.set_Item(4, cIndex, data.Columns[cIndex - 1].Caption);
            if (rows > 0)
            {
    
                //select the range where the data will be pasted
                Range r = sheet.get_Range(sheet.Cells[5, 1], sheet.Cells[5 + (rows - 1), columns]);
    
                //Convert the datatable to an object array
                object[,] workingValues = new object[rows, columns];
    
                for (int rIndex = 0; rIndex < rows; rIndex++)
                    for (int cIndex = 0; cIndex < columns; cIndex++)
                        workingValues[rIndex, cIndex] = data.Rows[rIndex][cIndex].ToString();
    
                r.Value2 = workingValues;
             }
    
    0 讨论(0)
  • 2020-12-17 03:12

    I would use a handler for the .xls file extension and a free component to convert the DataTable to native xls format. The component from this site http://www.csvreader.com/ does more that the URL implies. The newest version of excel will complain about an HTML formatted XLS file. Also keep in mind the size of the data being returned. Your web server should use compression for this extension and your code should check if the number of rows returned is greater than what excel can display in one worksheet; multiple sheets may be required. http://www.mrexcel.com/archive2/23600/26869.htm

    0 讨论(0)
  • 2020-12-17 03:23

    Change the page's file type to excel, and only stream the HTML necessary to build a table to the page. code from here

    //for demo purpose, lets create a small datatable & populate it with dummy data
    System.Data.DataTable workTable = new System.Data.DataTable();
    
    //The tablename specified here will be set as the worksheet name of the generated Excel file. 
    workTable.TableName = "Customers";
    workTable.Columns.Add("Id");
    workTable.Columns.Add("Name");
    System.Data.DataRow workRow;
    
    for (int i = 0; i <= 9; i++)
    {
    workRow = workTable.NewRow();
    workRow[0] = i;
    workRow[1] = "CustName" + i.ToString();
    workTable.Rows.Add(workRow);
    }
    
    //...and lets put DataTable2ExcelString to work
    string strBody = DataTable2ExcelString(workTable);
    
    Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
    Response.AppendHeader("Content-disposition", "attachment; filename=my.xls");
    Response.Write(strBody);
    
    0 讨论(0)
  • 2020-12-17 03:25

    If you create a page that is just a table with the results and set the page's content type to "application/vnd.ms-excel", then the output will be in Excel.

    Response.ContentType = "application/vnd.ms-excel";
    

    If you want to force a save, you would do something like the following:

    Response.AddHeader("Content-Disposition", "attachment; filename=somefilename.xls");
    
    0 讨论(0)
  • 2020-12-17 03:29

    I got a utils function that does this already. Once you put it into a datatable, you can export it with the Response using

            public static void DataTabletoXLS(DataTable DT, string fileName)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "utf-16";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
            HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", fileName));
            HttpContext.Current.Response.ContentType = "application/ms-excel";
    
            string tab = "";
            foreach (DataColumn dc in DT.Columns)
            {
                HttpContext.Current.Response.Write(tab + dc.ColumnName.Replace("\n", "").Replace("\t", ""));
                tab = "\t";
            }
            HttpContext.Current.Response.Write("\n");
    
            int i;
            foreach (DataRow dr in DT.Rows)
            {
                tab = "";
                for (i = 0; i < DT.Columns.Count; i++)
                {
                    HttpContext.Current.Response.Write(tab + dr[i].ToString().Replace("\n", "").Replace("\t", ""));
                    tab = "\t";
                }
                HttpContext.Current.Response.Write("\n");
            }
            HttpContext.Current.Response.End();
                   }
    
    0 讨论(0)
  • 2020-12-17 03:29

    Kindly use this code to resolve your problem.This code will convert excel sheet to text format.Hope this will solve your problem

        grdSrcRequestExport.RenderControl(oHtmlTextWriter);
        string s = "";
        s=oStringWriter.ToString().Replace("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">", "");
        s="<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head><meta http-equiv=Content-Type content=\"text/html; charset=us-ascii\"><meta name=ProgId content=Excel.Sheet><meta name=Generator content=\"Microsoft Excel 11\"><table x:str border=0 cellpadding=0 cellspacing=0 width=560 style='border-collapse: collapse;table-layout:fixed;width:420pt'>"+s.ToString()+"</table></body></html>";
        //Byte[] bContent = System.Text.Encoding.GetEncoding("utf-8").GetBytes();
        Response.Write(s);
    
    0 讨论(0)
提交回复
热议问题