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 items below the POI: HSSF is used to implement Excel read and write. The following is the homepage of HSSF
Http://jakarta.apache.org/poi/hssf/index.html
The following introduction is based on the following address:
Http://jakarta.apache.org/poi/hssf/quick-guide.html
The current version is 1.51 should be a stable version within a long time, but the SAMPLE provided by HSSF is not based on 1.51, so it takes appropriate attention when using it. In fact, several sub-projects below POI side have different reads Word HDF is developing the FOP under .xml (
Http://xml.apache.org/fop/index.html
) Output PDF files, is also a better tool directory: Create a Workbook to create a Sheet Create Cells Create Date Cells Setting cell format Description: The following may be required to use the following class
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 Workbook
HSSFWORKBOOK WB = New HSSFWORKBOOK (); // Create WorkbookFileoutputStream Fileout = New FileoutputStream ("Workbook.xls"); // Specify file name wb.write (fileout); // Output to file fileout.close ); create a sheetHSSFWorkbook wb = new HSSFWorkbook (); HSSFSheet sheet1 = 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 cells
HSSFWORKBOOK WB = New HSSFWORKBOOK (); hssfsheet sheet = wb.createsheet ("new sheet"); // Note that the following code Many methods are short instead of int, so you need to do a type conversion hssfrow rotion = shop = sheet.createrow (((( Short) 0); // Sheet Creates a line of hssfcell cell = row.createcell ((short) 0); // Create a cell Cell.SetCellValue (1); // Set the value of the value of the cell // value 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; hssfrow rot = sheet.createrow ((short) 0); hssfcell cell = row.createcell ((Short) 0); // Set value is the date Cell .SetcellValue (new date ()); hssfcellstyle cellstyle = wb.createcellStyle (); // Specify date display format cellstyle.SetDataFormat (HSSFDataFormat.getFormat ("m / d / yy h: mm"); cell = row.createcell ((Short) 1); Cell.SetcellValue (new date ()); // Setting the cell date 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 including cell alignment, content The font settings, the background color of the cell, etc.