Send query results to Excel from ASP.NET website

后端 未结 7 590
温柔的废话
温柔的废话 2020-12-17 02:49

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:31

    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)
提交回复
热议问题