Simple use of POI and simple use of POI

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.