The Excel method

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.OleDb;usingSystem.Windows.Forms;usingSystem.Data;usingSystem.Runtime.InteropServices;namespacewmsclient{ Public classExcel { Public Static voidSaveasexcel (DataTable dtexcel) {SaveFileDialog SaveFileDialog=NewSaveFileDialog (); Savefiledialog.filter="export Excel (*.xls) |*.xls"; Savefiledialog.filterindex=0; Savefiledialog.restoredirectory=true; Savefiledialog.createprompt=true; Savefiledialog.title="Export File Save path";            Savefiledialog.showdialog (); stringStrName =Savefiledialog.filename; if(Strname.length! =0)            {                //Export to ExeclSystem.Reflection.Missing Miss =System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application Excel=NewMicrosoft.Office.Interop.Excel.Application (); Try{Excel. APPLICATION.WORKBOOKS.ADD (true); Excel. Visible=false;//If True, the Excel interface will be displayed when exporting.                     if(Excel = =NULL) {MessageBox.Show ("Excel cannot start! ","Error", MessageBoxButtons.OK, Messageboxicon.error); return; } Microsoft.Office.Interop.Excel.Workbooks Books=(Microsoft.Office.Interop.Excel.Workbooks) Excel.                    Workbooks; Microsoft.Office.Interop.Excel.Workbook Book=( Microsoft.Office.Interop.Excel.Workbook) (books.                    ADD (Miss)); Microsoft.Office.Interop.Excel.Worksheet sheet=(Microsoft.Office.Interop.Excel.Worksheet) book.                    ActiveSheet; Sheet. Name="Test"; intm =0, n =0; //Generate column Name Here I is starting from 1 because my No. 0 column is a hidden column ID no need to write it in.                     for(inti =0; i < DtExcel.Columns.Count; i++) {Excel. cells[1, i +1] =Dtexcel.columns[i].                    Caption.tostring (); }                    //Populating Data                     for(inti =0; i < DtExcel.Rows.Count; i++)                    {                        //J is also starting from 1 reasons as everyone needs different                         for(intj =0; J < DtExcel.Columns.Count; J + +)                        {                            if(Dtexcel.rows[i][j]. ToString (). GetType () = =typeof(string) ) {Excel. Cells[i+2+ M +1] ="'"+Dtexcel.rows[i][j]. ToString ().                            Trim (); }                            Else{Excel. Cells[i+2, J +1] =Dtexcel.rows[i][j]. ToString ().                            Trim (); }}} sheet. SaveAs (StrName, Miss, Miss, Miss, Miss, Miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Miss, Miss                    , Miss); Book. Close (false, Miss, Miss); Books.                    Close (); Excel.                    Quit ();                    System.Runtime.InteropServices.Marshal.ReleaseComObject (sheet);                    System.Runtime.InteropServices.Marshal.ReleaseComObject (book);                    System.Runtime.InteropServices.Marshal.ReleaseComObject (books);                    System.Runtime.InteropServices.Marshal.ReleaseComObject (Excel); Gc.                    Collect (); MessageBox.Show ("The data has been exported successfully!","Tips", MessageBoxButtons.OK, messageboxicon.information); //toolstripprogressbar1.value = 0;System.Diagnostics.Process.Start (strName); }                Catch(Exception ex) {MessageBox.Show (ex). Message,"Error Hints"); }                finally{killspecialexcel (Excel); }            }        }        Private Static voidKillspecialexcel (Microsoft.Office.Interop.Excel.Application m_objexcel) {Try            {                if(M_objexcel! =NULL)                {                    intLpdwprocessid; GetWindowThreadProcessId (NewINTPTR (M_objexcel.hwnd), outlpdwprocessid); System.Diagnostics.Process.GetProcessById (LPDWPROCESSID).                Kill (); }            }            Catch(Exception ex) {Console.WriteLine ("Delete Excel Process Error:"+Ex.            Message); }} [DllImport ("User32.dll", CharSet =CharSet.Auto)]Static extern intGetWindowThreadProcessId (IntPtr hWnd, out intlpdwprocessid); }}

The Excel method

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.