Apache Project: POI-HSSF - Java API To Access Microsoft Excel Format Files (ZT)

zhaozj2021-02-08  446

Introduction: Jakarta_poi uses Java to read and write Excel (97-2002) files to meet most of the needs. Because there is a project to use this tool, I spent some time to translate a guide with the POI itself. There are some sections and modifications, I hope to give some people to use someone to get home help.

There are several self-projects under POI: HSSF is used to implement Excel's read and write. The following is the homepage of HSSF http://jakarta.apache.org/poi/hssf/index.html The following information is based on the following address: HTTP : //jakarta.apache.org/poi/hssf/quick-Guide.html The current version of 1.51 should be a stable version within a long time, but the hssf provided by the SAMPLE is not based on 1.51, so when used It is necessary to pay attention. In fact, several sub-projects below POI focused on different read and write Word HDF is being developed. XML's FOP (http://xml.apache.org /fop/index.html) can output PDF files, too A better tool directory: Create a Workbook Create a Sheet Create Cells Creating a Date Cells Settings Unit Format Description: The following may be required to use the following IMPORT Org.apache.poi.hssf.usermodel.hssfcell; import org.apache .poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org .apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; create workbookHSSFWorkbook wb = new HSSFWorkbook (); // use the default Construction Method Creating WorkbookFileOutputStream Fileout = New FileOutputStream ("Workbook.xls"); // Specify file name wb.write (fileout); // Output to file fileout.close (); create a SheethssFworkBook WB = New HSSFWorkbook (); HSSFSheet S heet1 = wb.createSheet ( "new sheet"); // workbook created sheetHSSFSheet sheet2 = wb.createSheet ( "second sheet"); // workbook created another sheetFileOutputStream fileOut = new FileOutputStream ( "workbook.xls"); wb. Write (fileout); fileout.close (); create cellshssfworkbook wb = new hssfworkbook (); hssfsheet sheet = wb.createsheet ("new sheet"); // Note The following code Many methods of parameters are short instead of int Do a type conversion hssfrow rot = sheet.createrow ((short) 0); // Sheet creates a line hssfcell cell = row.createcell ((Short) 0); // Create a cell Cell.setcellValue (1); / / Set the value of the value of the cell // Value of the value of the value of the parameters, there are many Double, String, Boolean, Row.createcell ((Short) 1) .SetCellvalue (1.2); row.createcell ((Short) 2) .setcellValue This is a string ");

Row.createcell ((Short) 3) .SetcellValue (TRUE); // Write the Output to a filefileoutputstream fileout = new fileoutputstream ("Workbook.xls"); wb.write (fileout); fileout.close (); creation date Cellshssfworkbook wb = new hssfworkbook (); hssfsheet sheet = wb.createsheet ("new sheet"); hssfrow rot = sheet.createrow (short); hssfcell cell = row.createceell (short) 0); // Set value Cell.SetcellValue (new date ()); hssfcellstyle cellstyle = wb.createcellStyle (); // Specify the date display format CellStyle.SetDataFormat (HSSFDataFormat.getFormat ("m / d / yy h: mm"); Cell = row.createcell ((Short) 1); Cell.SetcellValue (new date ()); // Setting cell dates display format cell.setcellStyle (cellstyle); fileoutputstream fileout = new fileoutputstream ("Workbook.xls") Wb.write (fileout); fileout.close (); setting cell format cell format setting There are many forms of forming a uniform alignment, the content of the content, the background color of the cell, because the form is compared More, just some examples. The following example may change in POI1.5 may change the specific viewing API ........... // aqua backgroundhssfcellstyle style = wb.createcellStyle (); // Create a Style style.setfillbackgroundColor (hssfcellstyle.aqua); // Setting the background color of this style populates Style.SetFillPattern (HSSFCellStyle.big_spots; // style fill type. // There are a variety of styles, such as: //HSSFCellStyle.BIG_SPOTS//HSSFCellStyle.FINE_DOTS//HSSFCellStyle.SPARSE_DOTS et style.setAlignment (HSSFCellStyle.ALIGN_CENTER); // centered style.setFillBackgroundColor (HSSFColor.GREEN.index); // Setting the cells background color style.setFillForegroundColor (HSSFCOLOR.Red.index); // Set cell display color hssfcell cell = row.createcell (short) 1); cell.setcellValue ("x"); cell.setcellStyle STYLE); HSSF to generate an Excel file after using POI files Workbook.write (OutputStream) Workbook.write (OutputStream) can be written to response.getoutputStream () If you set up Response's ContentType for Excel and Download the attachment name to download Excel

HSSFWorkbook book = _proxy.expertExcel (_formBean, _login); if (book = null!) {Response.setContentType ( "application / ms-excel"); response.setHeader ( "Content-Disposition", "attachment; filename =" New String ("Export Excel.xls" .GetBytes (), "ISO-8859-1)); book.write (response.getoutputstream ());} where ExpertExcel is nothing more than getting data from the database or other places to create Excel can.

转载请注明原文地址:https://www.9cbs.com/read-815.html

New Post(0)