《Java使用Apache POI導(dǎo)出Excel》要點:
本文介紹了Java使用Apache POI導(dǎo)出Excel,希望對您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:apache配置
維易PHP培訓(xùn)學(xué)院每天發(fā)布《Java使用Apache POI導(dǎo)出Excel》等實戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
Apache POI 是用Java 編寫的免費開源的跨平臺的 Java API,Apache POI提供API給Java程式對 Microsoft Office 格式檔案讀和寫的功能
HSSF 提供讀寫Microsoft Excel XLS格式檔案的功能.
XSSF 提供讀寫Microsoft Excel OOXML XLSX格式檔案的功能.
HWPF 提供讀寫Microsoft Word DOC格式檔案的功能.
HSLF 提供讀寫Microsoft PowerPoint格式檔案的功能.
HDGF 提供讀Microsoft Visio格式檔案的功能.
HPBF 提供讀Microsoft Publisher格式檔案的功能.
HSMF 提供讀Microsoft Outlook格式檔案的功能.
1.環(huán)境配置:導(dǎo)入jar包
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.16</version></dependency>
2.創(chuàng)立一個Excel工作簿
@Test public void test() throws IOException { //定義一個工作蒲 Workbook wb = new HSSFWorkbook(); //定義一個輸出流 FileOutputStream fileOutputStream = new FileOutputStream("/home/ubuntu/Desktop/Excel工作蒲.xls"); //寫入在輸出流 wb.write(fileOutputStream); //關(guān)閉輸出流 fileOutputStream.close(); }
3.創(chuàng)立一個sheet頁
@Test public void sheet() throws IOException { //定義一個工作蒲 Workbook wb = new HSSFWorkbook(); //創(chuàng)立sheet頁面 wb.createSheet("第一個sheet頁"); wb.createSheet("第二個sheet頁"); //定義一個輸出流 FileOutputStream fileOutputStream = new FileOutputStream("/home/ubuntu/Desktop/Excel工作蒲帶有sheet頁.xls"); //寫入在輸出流 wb.write(fileOutputStream); //關(guān)閉輸出流 fileOutputStream.close(); }
sheet頁
4.創(chuàng)立行和列
@Test public void row() throws IOException { //定義一個工作蒲 Workbook wb = new HSSFWorkbook(); //創(chuàng)立sheet頁面 Sheet sheet = wb.createSheet("學(xué)生信息sheet頁"); //創(chuàng)立一行 Row row = sheet.createRow(0); //創(chuàng)立一個單元格 Cell cell =null; for(int i = 0 ;i<5;i++){ row.createCell(i).setCellValue("寫入信息:單元格內(nèi)容"+i); } //定義一個輸出流 FileOutputStream fileOutputStream = new FileOutputStream("/home/ubuntu/Desktop/Excel學(xué)生信息.xls"); //寫入在輸出流 wb.write(fileOutputStream); //關(guān)閉輸出流 fileOutputStream.close(); }
5.創(chuàng)立一個時間樣式到Excel
@Test public void date() throws IOException { //定義一個工作蒲 Workbook wb = new HSSFWorkbook(); //創(chuàng)立sheet頁面 Sheet sheet = wb.createSheet("時間sheet頁"); //創(chuàng)立一行 Row row = sheet.createRow(0); //創(chuàng)立一個單元格 Cell cell = row.createCell(0); cell.setCellValue(new Date()); CreationHelper creationHelper = wb.getCreationHelper(); //設(shè)置單元格樣式 CellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("YYYY-MM-DD hh:mm:ss")); cell = row.createCell(1); cell.setCellValue(new Date()); //設(shè)置日期樣式 cell.setCellStyle(cellStyle); //定義一個輸出流 FileOutputStream fileOutputStream = new FileOutputStream("/home/ubuntu/Desktop/Excel日期格式.xls"); //寫入在輸出流 wb.write(fileOutputStream); //關(guān)閉輸出流 fileOutputStream.close(); }
6.單元格對其方式及行高
@Test public void style() throws IOException { //定義一個工作蒲 Workbook wb = new HSSFWorkbook(); //創(chuàng)建sheet頁面 Sheet sheet = wb.createSheet("第一個sheet"); //創(chuàng)建一行 Row row = sheet.createRow(0); //設(shè)置行高 row.setHeightInPoints(30); //創(chuàng)建一個單元格 createCell(wb,row,(short)0,HSSFCellStyle.ALIGN_CENTER,HSSFCellStyle.VERTICAL_BOTTOM); createCell(wb,row,(short)1,HSSFCellStyle.ALIGN_JUSTIFY,HSSFCellStyle.VERTICAL_CENTER); createCell(wb,row,(short)2,HSSFCellStyle.ALIGN_CENTER_SELECTION,HSSFCellStyle.VERTICAL_JUSTIFY); //定義一個輸出流 FileOutputStream fileOutputStream = new FileOutputStream("/home/ubuntu/Desktop/Excel樣式.xls"); //寫入在輸出流 wb.write(fileOutputStream); //關(guān)閉輸出流 fileOutputStream.close(); } /** * 創(chuàng)建一個單元格設(shè)置對應(yīng)的對其方式 * @param workbook 工作蒲 * @param row 行 * @param column 列 */ private static void createCell(Workbook workbook, Row row, short column,short halign,short valign){ Cell cell = row.createCell(column);//創(chuàng)建單元格 cell.setCellValue(new HSSFRichTextString("我是富文本"));//設(shè)置值 CellStyle cellStyle = workbook.createCellStyle();//創(chuàng)建樣式 cellStyle.setAlignment(halign);//設(shè)置單元格程度方向?qū)ζ浞绞?cellStyle.setVerticalAlignment(valign);//設(shè)置單元格垂直方向?qū)ζ浞绞?cell.setCellStyle(cellStyle); }
合并單元格
@Test public void test1() throws IOException { //定義一個工作蒲 Workbook wb = new HSSFWorkbook(); //創(chuàng)立sheet頁面 Sheet sheet = wb.createSheet("第一個sheet"); //創(chuàng)立一行 Row row = sheet.createRow(1); //設(shè)置行高 row.setHeightInPoints(30); //創(chuàng)立一個單元格 Cell cell = row.createCell(1); cell.setCellValue("合并單元格"); //合并單元格(起始行,結(jié)束行,起始列,結(jié)束列) sheet.addMergedRegion(new CellRangeAddress(1,2,1,2)); //定義一個輸出流 FileOutputStream fileOutputStream = new FileOutputStream("/home/ubuntu/Desktop/Excel樣式.xls"); //寫入在輸出流 wb.write(fileOutputStream); //關(guān)閉輸出流 fileOutputStream.close(); }
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/11763.html