WPF Excel export Method

Source: Internet
Author: User

Is to use WPF to export data into Excel and other. NET applications are the same, Asp. both net and winform are implemented in the same way. The only difference is ASP.. NET may have user permissions. After all, Asp.. Net execution users are specified by IIS, rather than default system users.

 

The specific implementation method is as follows,CodeUsing a complete namespace for ease of understanding

 

Step 1: Do not reference ExcelProgramSet, different from other onlineArticle, I directly referenced.. net. office. interOP. excel, instead of referencing Microsoft Excel in COM, install vs08 or.. NET Framework Package later. net itself has this Assembly and does not need to reference the Office Assembly installed in the system. Of course, both references have the same effect.

 

Then you can write the code directly.

 

 

DataoperationDataop =New Dataoperation();

 

DataviewDV = dataop. opertaionsget ();// Obtain a dataview. This is the method in my program. Here, you can obtain any dataview or other data set sheet.

 

Dataop. Clear ();

 

 

Microsoft. Office. InterOP. Excel.ApplicationclassAC =NewMicrosoft. Office. InterOP. Excel.Applicationclass();

Microsoft. Office. InterOP. Excel.WorkbookWB;// Here, never use new for instance objects. Otherwise, an exception will occur.

 

Microsoft. Office. InterOP. Excel.WorksheetWS;

 

 

 

 

WB = ac. workbooks. add (system. reflection. missing . value); // create a workbook (workbook: the subject of an Excel file)

Ws = (Microsoft. office. interOP. excel. worksheet ) WB. worksheets. add (system. reflection. missing . value, system. reflection. missing . value, system. reflection. missing . value, system. reflection. missing . value); // create a worksheet (Worksheet: Worksheet, that is, the sheet of the subtable in Excel)

 

// Set the table name

WS. Name ="Testxls";

 

// Import data to the cell of the worksheet

For(IntI = 0; I <DV. Count; I ++)

{

For(IntJ = 0; j <DV. Table. Columns. Count; j ++)

WS. cells [I + 1, J + 1] = DV [I] [J]. tostring ();

}

 

// Save to file

WB. saveas ( "D: // aa1.xls" , System. reflection. Missing . Value, system. reflection. Missing . Value, system. reflection. Missing . Value, system. reflection. Missing . Value, system. reflection. Missing . Value, Microsoft. Office. InterOP. Excel. Xlsaveasaccessmode . Xlnochange, system. reflection. Missing . Value, system. reflection. Missing . Value, system. reflection. Missing . Value, system. reflection. Missing . Value, system. reflection.Missing . Value );

 

// Close

WB. Close (system. reflection.Missing. Value, system. reflection.Missing. Value, system. reflection.Missing. Value );

 

 

In fact, it is very easy to import a data table to another data table, just pay attention to the parameters in the Excel method.

 

In this example, most parameters use the default value system. reflection. Missing. value. In the Save method, only 1st parameters must be the complete save path string and 7th parameters must be xlsaveasaccessmode enumeration.

 

Note that the cell index of the worksheet unit starts from 1, which is different from the previous one. Therefore, the code in the cells two-dimensional array isWS. cells [I + 1, J + 1.

 

Finally, close the worksheet, which is easy to understand. Otherwise, the saved files will be occupied by the Excel process all the time.

 

In addition, sometimes the numeric text is displayed in scientific notation.

WS. cells. numberformat = "@";

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.