Exporting data to excel in. NET uses more of the Npoi class library, but the Npoi version exists more, and the change is large, the official code is not quite complete, so here's a simple record.
First, the class library package used:
Using Npoi. Ss. Usermodel;using Npoi. HSSF. Usermodel;
II. Create a worksheet (3)
Hssfworkbook Hssfworkbook = new Hssfworkbook (); isheet sheet = Hssfworkbook. Createsheet ("Sheet1"); Hssfworkbook. Createsheet ("Sheet2"); Hssfworkbook. Createsheet ("Sheet3");
Use CreateRow () to create a row object, Createcell () to create a Cell object, and then set the contents of the cell by Setcellvalue ().
The following is a simple example of setting the caption font for a worksheet, and merging the first row, Top 5 columns
Title IRow row = sheet. CreateRow (0); Create a Row object icell cell = row. Createcell (0); Create Cell object cells . Setcellvalue (title); Set cell contents //Set cell font icellstyle style = Hssfworkbook. Createcellstyle (); Style. Alignment = Npoi. Ss. UserModel.HorizontalAlignment.Center; IFont font = Hssfworkbook. CreateFont (); Font. fontheight = *; Style. SetFont (font); Cell. CellStyle = style; Merges the contents of columns No. 0 through 4th sheet. Addmergedregion (New Npoi. Ss. Util.cellrangeaddress (0,0,0,4));
Iv. writing the contents of the Workbook object to a file
filestream</span> file = new FileStream (FilePath, filemode.create); Hssfworkbook. Write (file); File. Close ();
The following is a simple example:
<summary>///Export the contents of DataGridView to Excel File///</summary>//<param name= "D GV ">datagridview Object </param>//<param name=" title "> Set title </param>//<param name=" fi Lepath "> Saved file path </param>//<returns></returns> public static int Savetoexcel (DATAGRIDV Iew DGV, string title, String filePath) {if (Dgv==null | | (DGV. Rows.Count = = 0 && dgv. Columns.count = = 0)) return 1; Hssfworkbook Hssfworkbook = new Hssfworkbook (); Isheet sheet = Hssfworkbook. Createsheet ("Sheet1"); Hssfworkbook. Createsheet ("Sheet2"); Hssfworkbook. Createsheet ("Sheet3"); Title IRow row = sheet. CreateRow (0); Icell cell = row. Createcell (0); Cell. Setcellvalue (title); Icellstyle style = Hssfworkbook. Createcellstyle (); Style. Alignment = Npoi. Ss. Usermodel.horizontalaLignment. Center; IFont font = Hssfworkbook. CreateFont (); Font. Fontheight = 20 * 20; Style. SetFont (font); Cell. CellStyle = style; Sheet. Addmergedregion (New Npoi. Ss. Util.cellrangeaddress (0,0,0,DGV. columns.count-1)); Header int R, C; row = sheet. CreateRow (1); for (c = 0; c < DGV. Columns.count; C + +) {row. Createcell (c). Setcellvalue (DGV. COLUMNS[C]. HeaderText); }//content for (r = 0; r < DGV. Rows.Count; r++) {row = sheet. CreateRow (r+2); for (c = 0; c < DGV. Columns.count; C + +) {cell = row. Createcell (c); if (Dgv[c,r]. Value!=null) cell. Setcellvalue (Dgv[c,r]. Value.tostring ()); }} r = 0; FileStream File=null; try { File = new FileStream (FilePath, FileMode.Create); Hssfworkbook. Write (file); } catch (Exception ex) {MessageBox.Show (ex. Message); R = 1; The finally {if (file!=null) file. Close (); } return R; }
NPOI2.1.1 Simple to use