Export dataset to excel and keep the format.

Source: Internet
Author: User

NOTE: Refer to the online code in this article, and modify some content for me.

Development Environment: C #2005 (winform)

To export dataset to excel, you must:

  1. The format remains unchanged.
  2. The Worksheet name is the name of the able.
  3. Fast.
  4. You do not need to save the file. After exporting the file, open the excle file directly.

(If it is not saved, the newly generated process cannot be killed. Therefore, it is saved first and then opened .)

I found some code on the Internet and found this problem after trying it:

  1. If it is opened directly, the generated Excel process cannot be killed.
  2. Other normal Excel processes will be killed.

The Code is as follows:

Using Microsoft. office. interOP. excel; /// <summary> /// cause dataset to excel /// </Summary> /// <Param name = "dataset"> dataset </param> Public static void exporttoexcel (Dataset dataset) {try {// create the Excel application object // applicationclass excelapp = new applicationclass (); string outputpath = system. windows. forms. application. startuppath + @ "/EXCEL/123.xls"; excel. application excelapp = new Microsoft. office. interOP. excel. application (); excelapp. displayalerts = false; // do not prompt when saving // system. diagnostics. process xlprc; system. diagnostics. process [] xlprcarray; xlprcarray = system. diagnostics. process. getprocessesbyname ("Excel"); // determines the process system for creating an Excel file. diagnostics. process newexcelprc = xlprcarray [0]; for (INT I = 0; I <= xlprcarray. length-1; I ++) {If (xlprcarray [I]. totalprocessortime. totalseconds <newexcelprc. totalprocessortime. totalseconds) {newexcelprc = xlprcarray [I] ;}}// create a new Excel Workbook workbook excelworkbook = excelapp. workbooks. add (type. missing); int sheetindex = 0; // copy each datatable foreach (system. data. datatable DT in dataset. tables) {// copy the datatable to an object array object [,] rawdata = new object [DT. rows. count + 1, DT. columns. count]; // copy the column names to the first row of the object array for (INT Col = 0; Col <DT. columns. count; Col ++) {rawdata [0, Col] = DT. columns [col]. columnname;} // copy the values to the object array for (INT Col = 0; Col <DT. columns. count; Col ++) {// Add 'if (DT. columns [col]. datatype = system. type. getType ("system. string ") {for (int row = 0; row <DT. rows. count; row ++) {rawdata [row + 1, Col] = "'" + dt. rows [row]. itemarray [col]. tostring () ;}} else {for (int row = 0; row <DT. rows. count; row ++) {rawdata [row + 1, Col] = DT. rows [row]. itemarray [col]. tostring () ;}}// calculate the final column letter string finalcolletter = string. empty; string colcharset = "abcdefghijklmnopqrstuvwxyz"; int colcharsetlen = colcharset. length; If (DT. columns. count> colcharsetlen) {finalcolletter = colcharset. substring (DT. columns. count-1)/colcharsetlen-1, 1);} finalcolletter + = colcharset. substring (DT. columns. count-1) % colcharsetlen, 1); // create a new sheet worksheet excelsheet = (worksheet) excelworkbook. sheets. add (excelworkbook. sheets. get_item (++ sheetindex), type. missing, 1, xlsheettype. xlworksheet); excelsheet. name = DT. tablename; // fast data export to Excel String excelrange = string. format ("A1: {0} {1}", finalcolletter, DT. rows. count + 1); excelsheet. get_range (excelrange, type. missing ). value2 = rawdata; // mark the first row as bold (range) excelsheet. rows [1, type. missing]). font. bold = true;} // save and close the workbook excelworkbook. saveas (outputpath, xlfileformat. xlworkbooknormal, type. missing, type. missing, type. missing, type. missing, xlsaveasaccessmode. xlexclusive, type. missing, type. missing, type. missing, type. missing, type. missing); excelworkbook. close (true, type. missing, type. missing); excelworkbook = NULL; // release the Application Object excelapp. quit (); excelapp = NULL; // collect the unreferenced objects GC. collect (); GC. waitforpendingfinalizers (); newexcelprc. kill (); // open the exported file process. start (outputpath);} catch (exception ex) {MessageBox. show (ex. message );}}

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.