Openxml: Asp.net use openxml to export excel.

Source: Internet
Author: User

First, install Openxml SDK 2.0 and then reference DocumentFormat. Openxml. dll.

Reference namespace

Using DocumentFormat. OpenXml. Packaging;
Using DocumentFormat. OpenXml;
Using DocumentFormat. OpenXml. Spreadsheet;
Using System. Linq;

Openxml can only generate Office2007 documents, and must be written in Linq.

About excel.xlsx:

The excel file in office 2007 is actually a zip file. After changing the extension name to .zip, we can see some xml files in it.

The main content of excel is in the xl directory, excel Data is saved in the sharedString. xml file, and the worksheets directory stores the corresponding files of worksheet.

Sometimes the file we generated cannot be opened normally. We can use this method to compare the differences between the files we can open and find out the cause of the error.

Openxml Excel structure:

1. Create SpreadsheetDocument, which is equivalent to excel file-level SpreadsheetDocument. Create;

2. Create a workbookpart. In the code, use the root elements, spreadsheet. addworkbookpart () equivalent to XML. Although it is the "add" method, you can only add one.

3. Initial workbookpart

Code
SpreadSheet = SpreadsheetDocument. Create (stream, SpreadsheetDocumentType. Workbook );
WorkbookPart workbookPart = spreadSheet. AddWorkbookPart ();
WorkbookPart. Workbook = new Workbook ();

4. Create a worksheetpart. This is the management class of the Excel sheet. This can be used in the SDK's how to method.

5. Create a sharedstringtablepart, which is used to manage the Excel content. No matter whether your data is in the Excel worksheet, the values in the cell are stored in the lateral stringtable and then associated by relationshipid. Note that this is not mentioned in the SDK. To create multiple worksheets, you must set the count and uniquecount values of the sharedstringtable. Otherwise, multiple worksheets are created, and an error is reported when the generated Excel file is opened.

Repeated stringpart. SharedStringTable = new SharedStringTable ();
Repeated stringpart. SharedStringTable. Count = 1;
Repeated stringpart. SharedStringTable. UniqueCount = 1;

6. Write cell value:

Write the value to the mongostringtable first, and participate in the insertsharedstringitem method in the SDK. the return value corresponds to the ID.

Then, use the sample method insertcellinworksheet provided by the SDK to create a new cell.

Finally, associate the value with the cell.

Code
Int index = InsertSharedStringItem (text, comment stringpart );
Cell cell = InsertCellInWorksheet (name, Convert. ToUInt32 (I + startx), worksheetPart );

Cell. cellvalue = new cellvalue (index. tostring ());
Cell. DataType = new EnumValue <CellValues> (CellValues. SharedString );
WorksheetPart. Worksheet. Save ();

Run the following worksheetPart. Worksheet. Save () for each cell ();

7. Create multiple Excel Worksheet:

Obtain the Sheets set element through workbookPart. Workbook

Create a worksheetpart

Create a Worksheet

Obtain the relationID of the new worksheetpart through workbookPart. GetIdOfPart

Create a new sheet with the relationID.

Append created sheet to sheets collection

Run worksheetPart. Worksheet. Save ();

 

Asp.net export openxml Excel

1.Add System. IO namespace reference

2. MemoryStream MS = new MemoryStream (); replace the conventional file stream with MemoryStream

3. Create an excel file for openxml

4. Download:

Code
Response. Clear ();
Response. ClearHeaders ();
Response. ContentEncoding = System. Text. Encoding. GetEncoding ("UTF-8 ");
Response. ContentType = "application/vnd. ms-excel.sheet.binary.macroEnabled.12 ";
Response. AddHeader ("Content-Disposition", "attachment; filename=datavie=xlsx ");

Ms. Position = 0;
Byte [] buffer = new byte [4096];
Int I = 0;
Long length = 0;
While (length <ms. Length)
{
I = Ms. Read (buffer, 0, 4096 );
Response. outputstream. Write (buffer, 0, I );
Length + = I;
};
Ms. Dispose ();
Response. Flush ();
Response. End ();

Note that I used the following method:

Byte [] buffer = Ms. getbuffer ();

Response. outputstream. Write (buffer, 0, buffer. Length );

This is problematic because memorystring has two lengths: one is the byte length and the other is the space occupied. Therefore, using getbuffer to generate byte [] is the length of the space occupied by memory, instead of the actual length of the content, the file generated by response cannot be opened normally.

 

I encapsulated openxml for you to download/files/skyfei/openxmlexcel.rar

 

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.