Massive data export (thanks for the warm help of the great Gods abroad)

Source: Internet
Author: User

Using system;using system.collections.generic;using system.io;using system.linq;using System.Data;using System.data.sqlclient;using system.reflection;using documentformat.openxml;using DocumentFormat.OpenXml.Packaging ; Using Documentformat.openxml.spreadsheet;namespace vankeweb.baseclass{public class exportToExcel {public        int rowspersheet = 10000;        Public DataTable Resultsdata=new DataTable (); public void Exporttoexcels (DataTable dt,string path) {DataTableReader reader = dt.                                        CreateDataReader ();                    int c = 0;                    BOOL Firsttime = true;                    Get the Columns names, types, this would help when we need to format the cells in the Excel sheet. DataTable Dtschema = reader.                    GetSchemaTable ();                    var listcols = new list<datacolumn> (); if (Dtschema! = null) {foreach (DataRow drow iN dtschema.rows) {string columnName = Convert.ToString (drow["ColumnName                            "]);                            var column = new DataColumn (ColumnName, (Type) (drow["DataType")); Column.                            Unique = (bool) drow["IsUnique"]; Column.                            AllowDBNull = (bool) drow["AllowDBNull"]; Column.                            AutoIncrement = (bool) drow["Isautoincrement"];                            Listcols.add (column);                        RESULTSDATA.COLUMNS.ADD (column);                     }}//Call Read before accessing data. while (reader.                        Read ()) {DataRow DataRow = Resultsdata.newrow (); for (int i = 0; i < Listcols.count; i++) {datarow[(listcols[i])] = R                        Eader[i]; } resultsdata.rows.ADD (DataRow);                        C + +;                            if (c = = Rowspersheet) {c = 0;                            Exporttooxml (Firsttime,path);                            Resultsdata.clear ();                        Firsttime = false; }} if (ResultsData.Rows.Count > 0) {E                        Xporttooxml (Firsttime,path);                    Resultsdata.clear ();                    }//Call Close when done reading. Reader.                                                           Close ();            private void Exporttooxml (bool firsttime,string path) {string fileName = path;             Delete the file if it exists.            if (firsttime && file.exists (filename)) {file.delete (filename); } UINT sheetID = 1; Start at thE first sheet in the Excel workbook.                if (firsttime) {//this is the first time of creating the Excel file and the first sheet.                Create a spreadsheet document by supplying the filepath.                By default, AutoSave = True, Editable = True, and Type = xlsx.                    Spreadsheetdocument spreadsheetdocument = spreadsheetdocument.                Create (FileName, Spreadsheetdocumenttype.workbook);                Add a workbookpart to the document.                Workbookpart Workbookpart = Spreadsheetdocument.addworkbookpart (); Workbookpart.                Workbook = new Workbook ();                Add a worksheetpart to the Workbookpart. var Worksheetpart = Workbookpart.                Addnewpart<worksheetpart> ();                var sheetdata = new Sheetdata ();                Worksheetpart.worksheet = new Worksheet (sheetdata);                var bold1 = new Bold (); CellFormat CF = nEW CellFormat ();                Add Sheets to the Workbook.                Sheets Sheets;                    Sheets = SpreadsheetDocument.WorkbookPart.Workbook.                Appendchild<sheets> (New Sheets ());                Append a new worksheet and associate it with the workbook.                        var sheet = new sheet () {Id = Spreadsheetdocument.workbookpart.                Getidofpart (worksheetpart), sheetID = sheetID, Name = "Sheet" + sheetID                }; Sheets.                Append (sheet);                Add Header Row.                var headerrow = new Row (); foreach (DataColumn column in resultsdata.columns) {var cell = new Cell {DataType = Ce llvalues.string, Cellvalue = new cellvalue (column.                    ColumnName)};                Headerrow.appendchild (cell);           } sheetdata.appendchild (HeaderRow);     foreach (DataRow row in resultsdata.rows) {var newRow = new row ();                        foreach (DataColumn col in resultsdata.columns) {var cell = new cell {DataType = cellvalues.string, Cellvalue = new Ce Llvalue (Row[col].                        ToString ())};                    Newrow.appendchild (cell);                } sheetdata.appendchild (NewRow); } workbookpart.                Workbook.save ();            Spreadsheetdocument.close (); } else {//Open the Excel file that we created before, and start to add sheets to it                .                var spreadsheetdocument = Spreadsheetdocument.open (FileName, true);                var workbookpart = Spreadsheetdocument.workbookpart; if (Workbookpart.                   Workbook = = null) Workbookpart.                Workbook = new Workbook (); var Worksheetpart = Workbookpart.                Addnewpart<worksheetpart> ();                var sheetdata = new Sheetdata ();                Worksheetpart.worksheet = new Worksheet (sheetdata);                var sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets; if (sheets. Elements<sheet> (). Any ()) {//set The new sheet id sheetID = sheets. Elements<sheet> ().                Max (s = = S.sheetid.value) + 1;                } else {sheetID = 1;                }//Append a new worksheet and associate it with the workbook.                        var sheet = new sheet () {Id = Spreadsheetdocument.workbookpart.                Getidofpart (worksheetpart), sheetID = sheetID, Name = "Sheet" + sheetID                }; Sheets. Append(sheet);                Add the header row here.                var headerrow = new Row (); foreach (DataColumn column in resultsdata.columns) {var cell = new Cell {DataType = Ce llvalues.string, Cellvalue = new cellvalue (column.                    ColumnName)};                Headerrow.appendchild (cell);                                } sheetdata.appendchild (HeaderRow);                    foreach (DataRow row in resultsdata.rows) {var newRow = new row ();                        foreach (DataColumn col in resultsdata.columns) {var cell = new cell {DataType = cellvalues.string, Cellvalue = new Cellval UE (ROW[COL).                        ToString ())};                    Newrow.appendchild (cell);                } sheetdata.appendchild (NewRow);         }       Workbookpart.                Workbook.save ();                Close the document.            Spreadsheetdocument.close (); }        }    }    }

Refusal of piracy

Massive data export (thanks for the warm help of the great Gods abroad)

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.