Export an Excel worksheet to import multiple datatables of a dataset to multiple sheets of an Excel worksheet.

Source: Internet
Author: User
/// <Summary>
/// Export an Excel worksheet to import multiple datatables of a dataset to multiple sheets of an Excel worksheet.
/// </Summary>
/// <Param name = "ds"> dataset </param>
/// <Param name = "tablenames"> name of each table in DS </param>
/// <Param name = "strexcelfilename"> export the excelname (yyyy-mm-dd.xls) </param>
Public void doexport (Dataset ds, string [] tablenames, string strexcelfilename)
{
Microsoft. Office. InterOP. Excel. Application Excel = new Microsoft. Office. InterOP. Excel. Application ();
Try
{
Excel. Visible = false;
// Set to prohibit the pop-up prompt box for saving and overwriting
Excel. displayalerts = false;
Excel. alertbeforeoverwriting = false;
// Add a workbook
Workbook book = excel. workbooks. Add (true );
// Add a worksheet
Worksheet sheets = (Microsoft. Office. InterOP. Excel. worksheet)
Book. worksheets. Add (missing. Value, missing. Value, 19, Microsoft. Office. InterOP. Excel. xlsheettype. xlworksheet );

For (INT I = 0; I <Ds. Tables. Count; I ++)
{
System. Data. datatable table = Ds. Tables [I];
// Obtain a worksheet
Worksheet sheet = book. worksheets [I + 1] As worksheet;
Int rowindex = 1;
Int colindex = 0;

Foreach (datacolumn Col in table. columns)
{
Colindex ++;
Sheet. cells [1, colindex] = col. columnname;
}
Foreach (datarow row in table. Rows)
{
Rowindex ++;
Colindex = 0;
Foreach (datacolumn Col in table. columns)
{
Colindex ++;
// Add a single quotation mark before a number.
String typename = row [col. columnname]. GetType (). tostring ();
Sheet. cells [rowindex, colindex] = typecheckadd (row [col. columnname]. tostring (), typename );
}
}
Sheet. Name = tablenames [I];
}

// Delete extra Sheet
For (int g = 1; G <= book. worksheets. Count; G ++)
{
Worksheet sheet = book. worksheets [g] As worksheet;
If (sheet. Name. substring (0, 5) = "sheet ")
{
Sheet. Delete ();
G --;
}
}
Book. Save ();
Book. saveas (strexcelfilename, missing. Value, missing. value,
Microsoft. Office. InterOP. Excel. xlsaveasaccessmode. xlnochange, missing. Value, missing. value );

Excel. Quit ();
Excel = NULL;
GC. Collect ();

Savexls (strexcelfilename); // save it to the selected path
}
Catch (exception E)
{
Response. Write (E. Message );
}
}
# If region is a large number, it must be enclosed in leading quotation marks to convert it into a string.
Public String typecheckadd (string cellcontent, string strtype)
{
String cellcontentadd;
Switch (strtype)
{
Case "system. String ":
Cellcontentadd = "'" + cellcontent;
Break;
Default:
Cellcontentadd = cellcontent;
Break;
}
Return cellcontentadd;
}
# Endregion
# Region Save the generated Excel file to the selected path
Protected void savexls (string fileurl)
{
System. Io. fileinfo = new system. Io. fileinfo (fileurl );
Response. Clear ();
Response. charset = "gb2312 ";
Response. contentencoding = system. Text. encoding. utf8;
Response. addheader ("content-disposition", "attachment; filename =" + server. urlencode (fileinfo. Name. tostring ()));
Response. addheader ("Content-Length", fileinfo. length. tostring ());
Response. contenttype = "application/octet-stream ";
Response. contentencoding = system. Text. encoding. default;
Response. writefile (fileurl );
}
# Endregion

For (INT I = 0; I <Ds. Tables. Count; I ++)
{
System. Data. datatable table = Ds. Tables [I];
// Obtain a worksheet
Worksheet sheet = book. worksheets [I + 1] As worksheet;
Int rowindex = 1;
Int colindex = 0;

Foreach (datacolumn Col in table. columns)
{
Colindex ++;
Sheet. cells [1, colindex] = col. columnname;
}
Foreach (datarow row in table. Rows)
{
Rowindex ++;
Colindex = 0;
Foreach (datacolumn Col in table. columns)
{
Colindex ++;
// Add a single quotation mark before a number.
String typename = row [col. columnname]. GetType (). tostring ();
Sheet. cells [rowindex, colindex] = typecheckadd (row [col. columnname]. tostring (), typename );
}
}
Sheet. Name = tablenames [I];
}

// Delete extra Sheet
For (int g = 1; G <= book. worksheets. Count; G ++)
{
Worksheet sheet = book. worksheets [g] As worksheet;
If (sheet. Name. substring (0, 5) = "sheet ")
{
Sheet. Delete ();
G --;
}
}
Book. Save ();
Book. saveas (strexcelfilename, missing. Value, missing. value,
Microsoft. Office. InterOP. Excel. xlsaveasaccessmode. xlnochange, missing. Value, missing. value );

Excel. Quit ();
Excel = NULL;
GC. Collect ();

Savexls (strexcelfilename); // save it to the selected path
}
Catch (exception E)
{
Response. Write (E. Message );
}
}
# If region is a large number, it must be enclosed in leading quotation marks to convert it into a string.
Public String typecheckadd (string cellcontent, string strtype)
{
String cellcontentadd;
Switch (strtype)
{
Case "system. String ":
Cellcontentadd = "'" + cellcontent;
Break;
Default:
Cellcontentadd = cellcontent;
Break;
}
Return cellcontentadd;
}
# Endregion
# Region Save the generated Excel file to the selected path
Protected void savexls (string fileurl)
{
System. Io. fileinfo = new system. Io. fileinfo (fileurl );
Response. Clear ();
Response. charset = "gb2312 ";
Response. contentencoding = system. Text. encoding. utf8;
Response. addheader ("content-disposition", "attachment; filename =" + server. urlencode (fileinfo. Name. tostring ()));
Response. addheader ("Content-Length", fileinfo. length. tostring ());
Response. contenttype = "application/octet-stream ";
Response. contentencoding = system. Text. encoding. default;
Response. writefile (fileurl );
}
# Endregion

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.