. Net DataSet export to Excel,. netdataset

Source: Internet
Author: User

. Net DataSet export to Excel,. netdataset
Public void CreateExcel (DataSet ds, string typeid, stringFileName) {HttpResponse resp; resp = Page. response; resp. contentEncoding = System. text. encoding. getEncoding ("GB2312"); resp. appendHeader ("Content-Disposition", "attachment; filename =" + FileName); stringcolHeaders = "", ls_item = ""; int I = 0;
// Define the table object and row object, and use DataSet to initialize the value of ableabledt = ds. tables [0]; DataRow [] myRow = dt. select (""); // when typeid = "1", the exported documents are in EXCEL format; if (typeid = "1") {// get the headers of each column in the data table, separated by \ t, the carriage return for (I = 0; I <dt. columns. count; I ++) {if (I = dt. columns. count-1) {colHeaders + = dt. columns [I]. caption. toString () + "\ n";} else {colHeaders + = dt. columns [I]. caption. toString () + "\ t" ;}}// write the obtained data to the HTTP output stream. resp. write (colHeaders); // data processing row by row foreach (DataRow row inmyRow) {// In the current row, data is obtained column by column and separated by \ t, the carriage return \ n for (I = 0; I <dt. columns. count; I ++ ){
If (I = dt. columns. count-1) {ls_item + = row [I]. toString () + "\ n";} else {ls_item + = row [I]. toString () + "\ t" ;}// write the data in the current row to the HTTP output stream, and leave ls_item empty for the downstream data resp. write (ls_item); ls_item = "" ;}} else {if (typeid = "2 ") {// export XML data directly from DataSet and write it to the resp in the HTTP output stream. write (ds. getXml () ;}// write the data in the buffer to resp in the HTTP header document. end ();}

 


C # net import and export data to an excel table

The following code imports and exports data from DataSet and excel. The specific data from the database to DataSet should be!
Reference Microsoft. Excel 11.0 in COM

1 public class ImportExportToExcel
2 {
3 private string strConn;
4
5 private System. Windows. Forms. OpenFileDialog openFileDlg = new System. Windows. Forms. OpenFileDialog ();
6 private System. Windows. Forms. SaveFileDialog saveFileDlg = new System. Windows. Forms. SaveFileDialog ();
7
8 public ImportExportToExcel ()
9 {
10 //
11 // TODO: add the constructor logic here
12 //
13 this. openFileDlg. DefaultExt = "xls ";
14 this. openFileDlg. Filter = "Excel file (*. xls) | *. xls ";
15
16 this. saveFileDlg. DefaultExt = "xls ";
17 this. saveFileDlg. Filter = "Excel file (*. xls) | *. xls ";
18
19}
20
21. Import from an Excel file to DataSet # region import from an Excel file to DataSet
22 ///// <summary>
23 ///// import a file from Excel
24 ////// </summary>
25 ///// <param name = "strExcelFileName"> Excel file name </param>
26 ///// <returns> return DataSet </returns>
27 // public DataSet ImportFromExcel (string strExcelFileName)
28 //{
29 // return doImport (strE ...... the remaining full text>

Net helps to export the excel method with code

I still don't know the specific version, but your code is indeed not standardized,

Public void CreateExcel (DataSet ds, string typeid, 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 = "";
Int I = 0;

// Define the table object and row object, and use DataSet to initialize its value
DataTable dt = ds. Tables [0];
DataRow [] myRow = dt. Select ("");
// When typeid = "1", the exported file is in EXCEL format; When typeid = "2", the exported file is in XML format.
If (typeid = "1 ")
{
// Obtain the titles of each column in the data table, separated by \ t, and a carriage return is added after the title of the last column.
For (I = 0; I <dt. Columns. Count-1; I ++)
ColHeaders + = dt. Columns [I]. Caption. ToString () + "\ t ";
ColHeaders + = dt. Columns [I]. Caption. ToString () + "\ n ";
// Write the obtained data to the HTTP output stream
Resp. Write (colHeaders );
// Process data row by row
Foreach (DataRow row in myRow)
{
// In the current row, data is obtained one by one, separated by \ t, and carriage return \ n is added at the end
For (I = 0; I <row. ItemArray. Length-1; I ++)
Ls_item + = row [I]. ToString () + "\ t ";
Ls_item + = row [I]. ToString () + "\ n ";
// Write the data in the current row to the HTTP output stream, and leave ls_item empty for downstream data
Resp. Write (ls_item );
Ls_item = "";
}
}
Else
{
If (typeid = "2 ")
{
// Export XML data directly from DataSet and write it to the HTTP output stream
Resp. Write (ds. GetXml ());
}
}
// Write the data in the buffer to the HTTP header file
Resp. End ();

}
References:... the remaining full text>

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.