Today, when I was working on a project, I used code to generate an Excel file with a certain style. I found a lot of information and finally solved the problem. In Excel, I set the format and merged cells. The following describes how to use the npoi class library to Operate Excel.
1. First, generate an Excel file in the memory. The Code is as follows:
Hssfworkbook book = new hssfworkbook ();
Isheet sheet = book. createsheet ("sheet1 ");
2. Create the rows and columns in the newly created sheet. The Code is as follows:
Irow ROW = sheet. createrow (INDEX); // The number of rows indicated by index. heightinpoints = 35; // The Row Height icell cell = row. createcell (0); // create the first cell column. setcellvalue ("set cell value ");
3. Set the font size, border, and merged cells of the cell style.
(1). Create a cell font style and size.
/// <Summary> /// obtain the font style /// </Summary> /// <Param name = "hssfworkbook"> Excel operation class </param> /// <param name = "fontname"> body name </param> // <Param name = "fontcolor"> fontcolor </param> /// <Param name = "fontsize"> font size </param> /// <returns> </returns> Public static ifont getfontstyle (hssfworkbook, string fontfamily, hssfcolor fontcolor, int fontsize) {ifont font1 = hssfworkbook. createfont (); If (string. I Snullorempty (fontfamily) {font1.fontname = fontfamily;} If (fontcolor! = NULL) {font1.color = fontcolor. getindex () ;}font1.isitalic = true; font1.fontheightinpoints = (short) fontsize; return font1 ;}
(2). Set the format of data displayed in Cells
Icell cell = row. createcell (1); icellstyle cellstylenum = excel. geticellstyle (book); idataformat formatnum = book. createdataformat (); cellstylenum. dataformat = formatnum. getformat ("0.00e + 00"); // set the cell format to scientific notation.
Cell. cellstyle = cellstylenum;
(3) create a cell border, background color, and alignment
/// <Summary> /// obtain the cell style /// </Summary> /// <Param name = "hssfworkbook"> Excel operation class </param> /// <param name = "font"> cell font </param> // <Param name = "fillforegroundcolor"> pattern color </param> // <Param name = "fillpattern ""> pattern style </param> /// <Param name =" fillbackgroundcolor "> cell background </param> /// <Param name =" ha "> vertical alignment </param> /// <Param name = "va"> vertical alignment </param> /// <returns> </returns> Public static icellstyle getcell Style (hssfworkbook, ifont font, hssfcolor regular, fillpatterntype fillpattern, hssfcolor fillbackgroundcolor, horizontalalignment ha, verticalalignment VA) {icellstyle cellstyle = hssfworkbook. createcellstyle (); cellstyle. fillpattern = fillpattern; cellstyle. alignment = ha; cellstyle. verticalalignment = Va; If (fillforegroundcolor! = NULL) {cellstyle. fillforegroundcolor = fillforegroundcolor. getindex ();} If (fillbackgroundcolor! = NULL) {cellstyle. fillbackgroundcolor = fillbackgroundcolor. getindex ();} If (font! = NULL) {cellstyle. setfont (font);} // There is a border cellstyle. borderbottom = cellbordertype. thin; cellstyle. borderleft = cellbordertype. thin; cellstyle. borderright = cellbordertype. thin; cellstyle. bordertop = cellbordertype. thin; return cellstyle ;}
(4). Merge Cells
/// <Summary> /// merge cells /// </Summary> /// <Param name = "sheet"> the sheet of the cells to be merged </param>/ // <Param name = "rowstart"> Start row index </param> // <Param name = "rowend"> end row index </param> /// <Param name = "colstart"> Start column index </param> // <Param name = "colend"> end column index </param> Public static void setcellrangeaddress (isheet sheet, int rowstart, int rowend, int colstart, int colend) {cellrangeaddress = new cellrangeaddress (rowstart, rowend, colstart, colend); sheet. addmergedregion (cellrangeaddress );}
4. output the Excel file
Filestream stream = file. openwrite (@ "F:/test.xls ");;
Book. Write (Stream );
Stream. Close ();
The above section uses npoi to dynamically generate EXCEL rows and columns, as well as cell styles. For details, refer to the demo to download.
PS: Detailed can refer to the http://tonyqus.sinaapp.com/tutorial
Cell format settings, refer to http://tech.ddvip.com/2009-03/1238143235112434.html
Http://wenku.baidu.com/view/035c53d0c1c708a1284a4444.html