Simple use of POI and simple use of POI
I. Introduction
You can use the POI tool to export office files such as word, excel, and ppt.
2. Sample Code
Package com. wang. test; import java. io. file; import java. io. fileOutputStream; import java. util. calendar; import java. util. date; 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. HSSFRow; import org. apache. poi. hssf. usermodel. HSSFSheet; import org. apache. poi. hssf. usermodel. H SSFWorkbook; public class Test {public static void main (String [] args) throws Exception {// create a workbook HSSFWorkbook wb = new HSSFWorkbook (); // create a worksheet HSSFSheet sheet1 = wb. createSheet ("first sheet"); HSSFSheet sheet2 = wb. createSheet ("second sheet"); // create rowHSSFRow row = sheet1.createRow (0); // create cell HSSFCell cell = row. createCell (0); cell. setCellValue (false); row. createCell (1 ). setCellValue (Calendar. getInstance (); row. createC Ell (2 ). setCellValue (new Date (); row. createCell (3 ). setCellValue (123456.654321); String str = "abcdefghi"; // create a data format object HSSFDataFormat = wb. createDataFormat (); // create a cell style HSSFCellStyle = wb. createCellStyle (); // format the style on the date. setDataFormat (format. getFormat ("yyyy-MM-dd HH: mm: ss"); // apply the style to the cell row. getCell (1 ). setCellStyle (style); row. getCell (2 ). setCellStyle (style); // format the double value of style = wb. createCellStyle (); style. SetDataFormat (format. getFormat ("#. 00"); row. getCell (3). setCellStyle (style); // set the column width. Note: The column width is relative to the sheet. Sheet1.setColumnWidth (1, 3000); // You can also automatically adjust the column width sheet1.autoSizeColumn (2); sheet1.setColumnWidth (4, 7000); // automatically wrap the text, display row = sheet1.createRow (1); row. createCell (0 ). setCellValue ("top left"); row. createCell (1 ). setCellValue ("intermediate"); row. createCell (2 ). setCellValue ("bottom right"); // sets the row Height. setHeightInPoints (50); sheet1.setColumnWidth (0, 5000); sheet1.setColumnWidth (1, 5000); sheet1.setColumnWidth (2, 5000); // set the alignment mode -- style = wb on the left. createCellStyle (); style. setAlignment (HSSFCellStyle. ALIGN_LEFT); style. setverticalignment (HSSFCellStyle. VERTICAL_TOP); row. getCell (0 ). setCellStyle (style); // set the alignment mode -- intermediate style = wb. createCellStyle (); style. setAlignment (HSSFCellStyle. ALIGN_CENTER); style. setverticalignment (HSSFCellStyle. VERTICAL_CENTER); row. getCell (1 ). setCellStyle (style); // sets the alignment mode -- bottom right style = wb. createCellStyle (); style. setAlignment (HSSFCellStyle. ALIGN_RIGHT); style. setverticalignment (HSSFCellStyle. VERTICAL_BOTTOM); row. getCell (2 ). setCellStyle (style); // key point: calculation column row = sheet1.createRow (3); row. createCell (0 ). setCellValue (13); row. createCell (1 ). setCellValue (45); row. createCell (2 ). setCellValue (25); row. createCell (3 ). setCellFormula ("sum (A4: C4)"); // export to the disk wb. write (new FileOutputStream (new File ("f:/poi.xls") ;}} effect: