Export dataset data to Excel and dataset data export

Source: Internet
Author: User

Export dataset data to Excel and dataset data export

1. Write the data to the HTTP output stream/the exported data is in one row.

Public void CreateExcel (DataSet ds, string FileName) {HttpResponse resp; resp = Page. response; resp. contentEncoding = System. text. encoding. getEncoding ("GB2312"); resp. appendHeader ("Content-Disposition", "attachment; filename =" + FileName); string colHeaders = "", ls_item = ""; // Define table objects and row objects, at the same time, use DataSet to initialize its value DataTable dt = ds. tables [0]; DataRow [] myRow = dt. select (); // It can be similar to dt. int I = 0; int cl = dt. columns. count; // obtain the titles of each column in the data table, separated by/t. The carriage return (I = 0; I <cl; I ++) is added after the title of the last column) {if (I = (cl-1) // Add/n {colHeaders + = dt to the last column. columns [I]. caption. toString () + "/n";} else {colHeaders + = dt. columns [I]. caption. toString () + "/t" ;}} resp. write (colHeaders); // the data information written to the HTTP output stream // the data processed row by row (DataRow row in myRow) {// write the data in the current row to the HTTP output stream, and leave ls_item empty for downstream data (I = 0; I <cl; I ++) {if (I = (cl-1) // Add/n {ls_item + = row [I] to the last column. toString () + "/n";} else {ls_item + = row [I]. toString () + "/t" ;}} resp. write (ls_item); ls_item = "";} resp. end ();}

2. Open an excel file to write data row by row

You need to add two com references.

Microsoft Office 15.0 Object Library

Microsoft Excel 15.0 Object Library

 

Public bool DataSetToExcel (DataSet dataSet, bool isShowExcle) {DataTable dataTable = dataSet. tables [0]; int rowNumber = dataTable. rows. count; int columnNumber = dataTable. columns. count; if (rowNumber = 0) {// MessageBox. show ("no data can be imported to an Excel file! "); Return false;} else {Microsoft. office. interop. excel. application excel = new Microsoft. office. interop. excel. application (); excel. application. workbooks. add (true); excel. visible = isShowExcle; // whether to open the Excel file for (int c = 0; c <rowNumber; c ++) {for (int j = 0; j <columnNumber; j ++) {excel. cells [c + 1, j + 1] = dataTable. rows [c]. itemArray [j] ;}} return true ;}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.