Npoi Action Excel 005: Write empty Excel (version WinForm)

Source: Internet
Author: User

Tag: Generate SHA End name Enter form Stream Dex GUID

The previous article has written a BS version number of the export Excel sample (http://blog.csdn.net/yysyangyangyangshan/article/details/47904119). There are a few changes to the CS version in the saved place. Look directly at the code such as the following:
      private void Button1_Click (object sender, EventArgs e) {//content to save.            The contents of the code generated here, and in practice can be read by the database,//or the contents of the page input datatable dt = new DataTable (); Dt.            Columns.Add ("serial number"); Dt.            Columns.Add ("name"); Dt.            Columns.Add ("Age"); Dt.            Columns.Add ("position"); for (int i = 0; i < 5; i++) {DataRow row = dt.                NewRow ();                row["serial number"] = i + 1;                row["name"] = "Test" + i;                Row["Age" = + I; row["position"] = i% 2 = = 0?                "Project Teacher": "Manager"; Dt.            Rows.Add (row); }//In order to better see how to use Npoi.            Two lines of headings are displayed here.            Show caption can see how to merge cell string maintitle = "Main title";            string secondtitle = "subtitle";            Save the Excel path, and the file name is generated with a GUID of string fileindex = System.AppDomain.CurrentDomain.BaseDirectory;            String tempexcel = FileIndex + @ "\excelfile\{0}.xls"; Tempexcel = string. Format (Tempexcel, System.Guid.NewGuid ());            int rowIndex = 0; Manipulate several main objects of Excel.            Declared here Hssfworkbook workbook = new Hssfworkbook (); Hssfsheet sheet = workbook.            Createsheet (); Row0 and Row1 are two lines of title Hssfrow row0 = Sheet.            CreateRow (RowIndex); Hssfcell cell0 = row0.            Createcell (0); Cell0.            Setcellvalue (Maintitle); Hssfcellstyle style = workbook.            Createcellstyle (); Style.            Alignment = Cellhorizontalalignment.center; Hssffont font = workbook.            CreateFont (); Font. Boldweight = short.            MaxValue; Style.            SetFont (font); Cell0.            CellStyle = style; Merge cell sheet here. Addmergedregion (New Npoi. HSSF.            Util.cellrangeaddress (RowIndex, RowIndex, 0, 5));            rowindex++; Hssfrow row1 = sheet.            CreateRow (RowIndex); Hssfcell cell1 = Row1.            Createcell (0); Cell1.            Setcellvalue (Secondtitle); Cell1.            CellStyle = style; Sheet. Addmergedregion (NEW Npoi. HSSF.            Util.cellrangeaddress (RowIndex, RowIndex, 0, 5)); Because the column name is already specified.            Occupy a line of rowindex++; This line shows the table header hssfrow Row2 = sheet.            CreateRow (RowIndex);            int row2cellindex = 0; foreach (DataColumn col in dt. Columns) {Hssfcell cell = row2.                Createcell (Row2cellindex); Cell. Setcellvalue (Col.                Columnname.tostring ());            row2cellindex++;            } rowindex++; The contents of the DataTable for (int i = 0; i < dt. Rows.Count; i++) {Hssfrow row = sheet.                CreateRow (RowIndex); foreach (DataColumn col in dt. Columns) {row. Createcell (Col. Ordinal). Setcellvalue (dt. Rows[i][col].                ToString ());            } rowindex++;            }//Use file stream to save MemoryStream ms = new MemoryStream (); Workbook.            Write (MS); Ms.            Flush (); Ms.         Position = 0;   workbook = null;                      sheet = null;            FolderBrowserDialog dialog = new FolderBrowserDialog (); Dialog.            Description = "Please select file path"; if (dialog.                ShowDialog () = = DialogResult.OK) {string saveFile = [email protected] "\testexcel.xls";                     using (FileStream fs = new FileStream (Tempexcel, FileMode.Create, FileAccess.ReadWrite)) { Ms.                 WriteTo (FS);                } file.copy (Tempexcel, SaveFile, true); MessageBox.Show ("Export succeeded.            ");            } if (File.exists (Tempexcel)) {file.delete (tempexcel); } Ms.        Close (); }

Save using the Save dialog box:
         FolderBrowserDialog dialog = new FolderBrowserDialog ();            Dialog. Description = "Please select file path";            if (dialog. ShowDialog () = = DialogResult.OK)            {                string saveFile = [email protected] "\testexcel.xls";                using (FileStream fs = new FileStream (Tempexcel, FileMode.Create, FileAccess.ReadWrite))                 {                     Ms. WriteTo (fs);                 }                File.Copy (Tempexcel, SaveFile, true);                MessageBox.Show ("Export succeeded! ");            }
This also enables you to export excel in a form.
Project Download: http://download.csdn.net/detail/yysyangyangyangshan/9039017

Npoi Action Excel 005: Write empty Excel (version WinForm)

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.