C # export an Excel file using ListView

Source: Internet
Author: User

Public void ExportToExecl () {System. windows. forms. saveFileDialog sfd = new SaveFileDialog (); sfd. defaultExt = "xls"; sfd. filter = "Excel file (*. xls) | *. xls "; if (sfd. showDialog () = DialogResult. OK) {DoExport (this. lvreport, sfd. fileName) ;}# region Kill Special Excel Process [DllImport ("user32.dll", SetLastError = true)] static extern int GetWindowThreadProcessId (IntPtr hWnd, out int lpdwProcessId); Private void DoExport (ListView listView, string strFileName) {int rowNum = listView. items. count; int columnNum = listView. items [0]. subItems. count; int rowIndex = 1; int columnIndex = 0; if (rowNum = 0 | string. isNullOrEmpty (strFileName) {return;} if (rowNum> 0) {Microsoft. office. interop. excel. application xlApp = new Microsoft. office. interop. excel. application (); if (xlApp = null) {Messa GeBox. show ("an excel object cannot be created, maybe excel is not installed in your system"); return;} xlApp. defaultFilePath = ""; xlApp. displayAlerts = true; xlApp. sheetsInNewWorkbook = 1; Microsoft. office. interop. excel. workbook xlBook = xlApp. workbooks. add (true); // import the column name of the ListView to the foreach (ColumnHeader dc in listView. columns) {columnIndex ++; xlApp. cells [rowIndex, columnIndex] = dc. text;} // import the data in ListView to Excel for (int I = 0; I <row Num; I ++) {rowIndex ++; columnIndex = 0; for (int j = 0; j <columnNum; j ++) {columnIndex ++; // note that the purpose of adding "\ t" to the export is to prevent the exported data from being displayed as scientific notation. It can be placed at the beginning and end of each line. XlApp. cells [rowIndex, columnIndex] = Convert. toString (listView. items [I]. subItems [j]. text) + "\ t" ;}// the exception must be noted that strFileName, Excel. xlFileFormat. when xlExcel9795 is saved, an error is returned when your Excel versions are not 95, 97, but 2003 and 2007. The exception is HRESULT: 0x800A03EC. The solution is to replace strFileName with Microsoft. Office. Interop. Excel. XlFileFormat. xlWorkbookNormal. XlBook. saveAs (strFileName, Microsoft. office. interop. excel. xlFileFormat. xlWorkbookNormal, Type. missing, Type. missing, Type. missing, Type. missing, Microsoft. office. interop. excel. xlSaveAsAccessMode. xlExclusive, Type. missing, Type. missing, Type. missing, Type. missing, Type. missing); xlApp = null; xlBook = null; MessageBox. show ("report exported successfully! "); Try {if (xlApp! = Null) {int lpdwPeocessId; GetWindowThreadProcessId (new IntPtr (xlApp. hwnd), out lpdwPeocessId); System. diagnostics. process. getprocpolicyid (lpdwPeocessId ). kill () ;}} catch (System. exception ex) {Console. writeLine ("Delete Excel Process Error:" + ex. message) ;}}# endregion

 

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.