日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POI学习笔记 自定义颜色

發(fā)布時間:2024/4/17 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POI学习笔记 自定义颜色 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

http://yunzhongxia.iteye.com/blog/561425

http://yunzhongxia.iteye.com/blog/558998

項目中經(jīng)常要解析和生成Excel文件,最常用的開源組件有poi與jxl。jxl是韓國人開發(fā)的,發(fā)行較早,但是更新的很慢,目前似乎還不支持excel2007。poi是apache下的一個子項目,poi應(yīng)該是處理ms的office系列文檔最好的組件了。poi3.6版本已經(jīng)開始支持excel2007了。但是由于excel2007底層的實現(xiàn)似乎變成xml與excel2003底層存儲發(fā)生了本質(zhì)的變化,因此poi解析excel的類就存在差異了。

????? 現(xiàn)在簡單的介紹下poi常用的接口。

????? 經(jīng)常用的類一般都在org.apache.poi.hssf.usermodel(excel2003)或org.apache.poi.xssf.usermodel
(excel2007)。

  • 工作薄:? WorkBook是操作Excel的入口,HSSFWorkbook, XSSFWorkbook實現(xiàn)了該接口。
  • 頁:Sheet表示工作薄的分頁。HSSFSheet, XSSFChartSheet, XSSFDialogsheet, XSSFSheet實現(xiàn)了該接口。
  • Row:表示頁中的一行。HSSFRow, XSSFRow實現(xiàn)了該接口。
  • Cell:行中的一個單元格。HSSFCell, XSSFCell實現(xiàn)了該接口。

從上面的介紹得知:頁是通過工作薄對象創(chuàng)建的,行是通過頁對象創(chuàng)建的,單元格是通過行對象創(chuàng)建的。接下來,我們就開始發(fā)掘poi的強大功能吧。

  • 創(chuàng)建一個空白的工作薄
  • Java代碼 ?
  • import?java.io.FileOutputStream; ??
  • import?org.apache.poi.hssf.usermodel.HSSFWorkbook; ??
  • import?org.apache.poi.ss.usermodel.Workbook; ??
  • import?org.apache.poi.xssf.usermodel.XSSFWorkbook; ??
  • ??
  • //2003版本 ??
  • Workbook?wb?=?new?HSSFWorkbook(); ??
  • FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • wb.write(fileOut); ??
  • ?fileOut.close(); ??
  • ??
  • //2007版本 ??
  • //?Workbook?wb?=?new?XSSFWorkbook(); ??
  • //?FileOutputStream?fileOut?=?new?FileOutputStrea("workbook.xlsx"); ??
  • //?wb.write(fileOut); ??
  • //?fileOut.close();??
  • import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;//2003版本 Workbook wb = new HSSFWorkbook(); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut);fileOut.close();//2007版本 // Workbook wb = new XSSFWorkbook(); // FileOutputStream fileOut = new FileOutputStrea("workbook.xlsx"); // wb.write(fileOut); // fileOut.close();

    ?注意:Workbook 是org.apache.poi.ss.usermodel包下的一個接口,注意與以前版本的不同,HSSFWorkbook和XSSFWorkbook實現(xiàn)了該接口,這樣就達到了面前接口編程。下面的excel工具類中要用到此點知識。

    ?2. 創(chuàng)建兩個空白頁

    ?

    ?

    Java代碼 ?
  • Workbook?wb?=?new?HSSFWorkbook(); ??
  • //Workbook?wb?=?new?XSSFWorkbook(); ??
  • Sheet?sheet1?=?wb.createSheet("new?sheet"); ??
  • Sheet?sheet2?=?wb.createSheet("second?sheet"); ??
  • FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • wb.write(fileOut); ??
  • fileOut.close();??
  • Workbook wb = new HSSFWorkbook(); //Workbook wb = new XSSFWorkbook(); Sheet sheet1 = wb.createSheet("new sheet"); Sheet sheet2 = wb.createSheet("second sheet"); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close();

    ?

    3. 創(chuàng)建單元格

    ?

    Java代碼 ?
  • public?void?createRow()?throws?Exception?{ ??
  • ????????Workbook?wb?=?new?HSSFWorkbook(); ??
  • ????????//?Workbook?wb?=?new?XSSFWorkbook(); ??
  • ????????CreationHelper?createHelper?=?wb.getCreationHelper(); ??
  • ????????Sheet?sheet?=?wb.createSheet("new?sheet"); ??
  • ??
  • ????????//創(chuàng)建一行并放一些單元格到該行中,行的索引是以0開始的 ??
  • ????????Row?row?=?sheet.createRow((short)?0); ??
  • ????????//?創(chuàng)建一個單元格并填充一個整數(shù)的值 ??
  • ????????Cell?cell?=?row.createCell(0); ??
  • ????????cell.setCellValue(1); ??
  • ??
  • ????????//鏈式寫法 ??
  • ????????row.createCell(1).setCellValue(1.2); ??
  • ????????row.createCell(2).setCellValue( ??
  • ????????????????createHelper.createRichTextString("This?is?a?string")); ??
  • ????????row.createCell(3).setCellValue(true); ??
  • ??
  • ????????//輸出文件 ??
  • ????????FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • ????????wb.write(fileOut); ??
  • ????????fileOut.close(); ??
  • ??
  • ????}??
  • public void createRow() throws Exception {Workbook wb = new HSSFWorkbook();// Workbook wb = new XSSFWorkbook();CreationHelper createHelper = wb.getCreationHelper();Sheet sheet = wb.createSheet("new sheet");//創(chuàng)建一行并放一些單元格到該行中,行的索引是以0開始的Row row = sheet.createRow((short) 0);// 創(chuàng)建一個單元格并填充一個整數(shù)的值Cell cell = row.createCell(0);cell.setCellValue(1);//鏈式寫法row.createCell(1).setCellValue(1.2);row.createCell(2).setCellValue(createHelper.createRichTextString("This is a string"));row.createCell(3).setCellValue(true);//輸出文件FileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}

    ?

    ?4. 創(chuàng)建日期單元格

    ?

    Java代碼 ?
  • public?void?createDateCell()?throws?Exception?{ ??
  • ????????Workbook?wb?=?new?HSSFWorkbook(); ??
  • ????????//?Workbook?wb?=?new?XSSFWorkbook(); ??
  • ????????CreationHelper?createHelper?=?wb.getCreationHelper(); ??
  • ????????Sheet?sheet?=?wb.createSheet("new?sheet"); ??
  • ??
  • ????????//?Create?a?row?and?put?some?cells?in?it.?Rows?are?0?based. ??
  • ????????Row?row?=?sheet.createRow(0); ??
  • ??
  • ????????//?Create?a?cell?and?put?a?date?value?in?it.?The?first?cell?is?not ??
  • ????????//?styled ??
  • ????????//?as?a?date. ??
  • ????????Cell?cell?=?row.createCell(0); ??
  • ????????cell.setCellValue(new?Date()); ??
  • ??
  • ????????//?we?style?the?second?cell?as?a?date?(and?time).?It?is?important?to ??
  • ????????//?create?a?new?cell?style?from?the?workbook?otherwise?you?can?end?up ??
  • ????????//?modifying?the?built?in?style?and?effecting?not?only?this?cell?but ??
  • ????????//?other?cells. ??
  • ????????CellStyle?cellStyle?=?wb.createCellStyle(); ??
  • ????????cellStyle.setDataFormat(createHelper.createDataFormat().getFormat( ??
  • ????????????????"yyyy/MM/dd?hh:mm")); ??
  • ????????cell?=?row.createCell(1); ??
  • ????????//?cell.setCellValue(new?Date()); ??
  • ????????Date?date?=?new?Date(); ??
  • ??
  • ????????cell.setCellValue(createHelper.createRichTextString(date.toString())); ??
  • ????????cell.setCellStyle(cellStyle); ??
  • ??
  • ????????//?you?can?also?set?date?as?java.util.Calendar ??
  • ????????cell?=?row.createCell(2); ??
  • ????????cell.setCellValue(Calendar.getInstance()); ??
  • ????????cell.setCellStyle(cellStyle); ??
  • ??
  • ????????//?Write?the?output?to?a?file ??
  • ????????FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • ????????wb.write(fileOut); ??
  • ????????fileOut.close(); ??
  • ??
  • ????}??
  • public void createDateCell() throws Exception {Workbook wb = new HSSFWorkbook();// Workbook wb = new XSSFWorkbook();CreationHelper createHelper = wb.getCreationHelper();Sheet sheet = wb.createSheet("new sheet");// Create a row and put some cells in it. Rows are 0 based.Row row = sheet.createRow(0);// Create a cell and put a date value in it. The first cell is not// styled// as a date.Cell cell = row.createCell(0);cell.setCellValue(new Date());// we style the second cell as a date (and time). It is important to// create a new cell style from the workbook otherwise you can end up// modifying the built in style and effecting not only this cell but// other cells.CellStyle cellStyle = wb.createCellStyle();cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy/MM/dd hh:mm"));cell = row.createCell(1);// cell.setCellValue(new Date());Date date = new Date();cell.setCellValue(createHelper.createRichTextString(date.toString()));cell.setCellStyle(cellStyle);// you can also set date as java.util.Calendarcell = row.createCell(2);cell.setCellValue(Calendar.getInstance());cell.setCellStyle(cellStyle);// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}

    ?

    5.? 創(chuàng)建不同的單元格樣式

    ?

    Java代碼 ?
  • public?void?createCellType()?throws?Exception{ ??
  • ????????Workbook?wb?=?new?HSSFWorkbook(); ??
  • ????????Sheet?sheet?=?wb.createSheet("new?sheet"); ??
  • ????????Row?row?=?sheet.createRow((short)2); ??
  • ????????row.createCell(0).setCellValue(1.1); ??
  • ????????row.createCell(1).setCellValue(new?Date()); ??
  • ????????row.createCell(2).setCellValue(Calendar.getInstance()); ??
  • ????????row.createCell(3).setCellValue("a?string"); ??
  • ????????row.createCell(4).setCellValue(true); ??
  • ????????row.createCell(5).setCellType(HSSFCell.CELL_TYPE_ERROR); ??
  • ??
  • ????????//?Write?the?output?to?a?file ??
  • ????????FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • ????????wb.write(fileOut); ??
  • ????????fileOut.close(); ??
  • ????}??
  • public void createCellType() throws Exception{Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("new sheet");Row row = sheet.createRow((short)2);row.createCell(0).setCellValue(1.1);row.createCell(1).setCellValue(new Date());row.createCell(2).setCellValue(Calendar.getInstance());row.createCell(3).setCellValue("a string");row.createCell(4).setCellValue(true);row.createCell(5).setCellType(HSSFCell.CELL_TYPE_ERROR);// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}

    ?

    6.? 設(shè)置單元格水平垂直對齊方式

    ?

    Java代碼 ?
  • public?static?void?main(String[]?args)??throws?Exception?{ ??
  • ????????Workbook?wb?=?new?XSSFWorkbook();?//or?new?HSSFWorkbook(); ??
  • ??
  • ????????Sheet?sheet?=?wb.createSheet(); ??
  • ????????Row?row?=?sheet.createRow((short)?2); ??
  • ????????row.setHeightInPoints(30); ??
  • ??
  • ????????createCell(wb,?row,?(short)?0,?XSSFCellStyle.ALIGN_CENTER,?XSSFCellStyle.VERTICAL_BOTTOM); ??
  • ????????createCell(wb,?row,?(short)?1,?XSSFCellStyle.ALIGN_CENTER_SELECTION,?XSSFCellStyle.VERTICAL_BOTTOM); ??
  • ????????createCell(wb,?row,?(short)?2,?XSSFCellStyle.ALIGN_FILL,?XSSFCellStyle.VERTICAL_CENTER); ??
  • ????????createCell(wb,?row,?(short)?3,?XSSFCellStyle.ALIGN_GENERAL,?XSSFCellStyle.VERTICAL_CENTER); ??
  • ????????createCell(wb,?row,?(short)?4,?XSSFCellStyle.ALIGN_JUSTIFY,?XSSFCellStyle.VERTICAL_JUSTIFY); ??
  • ????????createCell(wb,?row,?(short)?5,?XSSFCellStyle.ALIGN_LEFT,?XSSFCellStyle.VERTICAL_TOP); ??
  • ????????createCell(wb,?row,?(short)?6,?XSSFCellStyle.ALIGN_RIGHT,?XSSFCellStyle.VERTICAL_TOP); ??
  • ??
  • ????????//?Write?the?output?to?a?file ??
  • ????????FileOutputStream?fileOut?=?new?FileOutputStream("xssf-align.xlsx"); ??
  • ????????wb.write(fileOut); ??
  • ????????fileOut.close(); ??
  • ??
  • ????} ??
  • ??
  • ????/** ?
  • ?????*?Creates?a?cell?and?aligns?it?a?certain?way. ?
  • ?????* ?
  • ?????*?@param?wb?????the?workbook ?
  • ?????*?@param?row????the?row?to?create?the?cell?in ?
  • ?????*?@param?column?the?column?number?to?create?the?cell?in ?
  • ?????*?@param?halign?the?horizontal?alignment?for?the?cell. ?
  • ?????*/??
  • ????private?static?void?createCell(Workbook?wb,?Row?row,?short?column,?short?halign,?short?valign)?{ ??
  • ????????Cell?cell?=?row.createCell(column); ??
  • ????????cell.setCellValue(new?XSSFRichTextString("Align?It")); ??
  • ????????CellStyle?cellStyle?=?wb.createCellStyle(); ??
  • ????????cellStyle.setAlignment(halign); ??
  • ????????cellStyle.setVerticalAlignment(valign); ??
  • ????????cell.setCellStyle(cellStyle); ??
  • ????}??
  • ?

    7.? 設(shè)置單元格的邊框

    ?

    Java代碼 ?
  • public?void?createBorder()?throws?Exception?{ ??
  • ????????Workbook?wb?=?new?HSSFWorkbook(); ??
  • ????????Sheet?sheet?=?wb.createSheet("new?sheet"); ??
  • ??
  • ????????//?Create?a?row?and?put?some?cells?in?it.?Rows?are?0?based. ??
  • ????????Row?row?=?sheet.createRow(1); ??
  • ??
  • ????????//?Create?a?cell?and?put?a?value?in?it. ??
  • ????????Cell?cell?=?row.createCell(1); ??
  • ????????cell.setCellValue(4); ??
  • ??
  • ????????//?Style?the?cell?with?borders?all?around. ??
  • ????????CellStyle?style?=?wb.createCellStyle(); ??
  • ????????style.setBorderBottom(CellStyle.BORDER_THIN); ??
  • ????????style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); ??
  • ????????style.setBorderLeft(CellStyle.BORDER_THIN); ??
  • ????????style.setLeftBorderColor(IndexedColors.GREEN.getIndex()); ??
  • ????????style.setBorderRight(CellStyle.BORDER_THIN); ??
  • ????????style.setRightBorderColor(IndexedColors.BLUE.getIndex()); ??
  • ????????style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); ??
  • ????????style.setTopBorderColor(IndexedColors.BLACK.getIndex()); ??
  • ????????cell.setCellStyle(style); ??
  • ??
  • ????????//?Write?the?output?to?a?file ??
  • ????????FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • ????????wb.write(fileOut); ??
  • ????????fileOut.close(); ??
  • ??
  • ????}??
  • public void createBorder() throws Exception {Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("new sheet");// Create a row and put some cells in it. Rows are 0 based.Row row = sheet.createRow(1);// Create a cell and put a value in it.Cell cell = row.createCell(1);cell.setCellValue(4);// Style the cell with borders all around.CellStyle style = wb.createCellStyle();style.setBorderBottom(CellStyle.BORDER_THIN);style.setBottomBorderColor(IndexedColors.BLACK.getIndex());style.setBorderLeft(CellStyle.BORDER_THIN);style.setLeftBorderColor(IndexedColors.GREEN.getIndex());style.setBorderRight(CellStyle.BORDER_THIN);style.setRightBorderColor(IndexedColors.BLUE.getIndex());style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);style.setTopBorderColor(IndexedColors.BLACK.getIndex());cell.setCellStyle(style);// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}

    ?

    8. 迭代行和單元格

    ?

    ??? 有時需要迭代一個頁中的所有行,或者一個行中所有的單元格。一個簡單的方法是循環(huán)。

    ??? 幸運的是,poi知道我們所需。頁可以通過sheet.rowIterator()迭代出所有的行,行可以通過row.cellIterator()迭代出所有的單元格??傊?#xff0c;Sheet和Row實現(xiàn)了java.lang.Iterable,如果你用的是jdk1.5以上的版本,你可以使用java高級for循環(huán)。

    ?

    Java代碼 ?
  • Sheet?sheet?=?wb.getSheetAt(0); ??
  • ????for?(Iterator?rit?=?sheet.rowIterator();?rit.hasNext();?)?{ ??
  • ????????Row?row?=?(Row)rit.next(); ??
  • ????????for?(Iterator?cit?=?row.cellIterator();?cit.hasNext();?)?{ ??
  • ????????????Cell?cell?=?(Cell)cit.next(); ??
  • ????????????//?Do?something?here ??
  • ????????} ??
  • ????} ??
  • ????????????????????HSSFSheet?sheet?=?wb.getSheetAt(0); ??
  • ????for?(Iterator<HSSFRow>?rit?=?(Iterator<HSSFRow>)sheet.rowIterator();?rit.hasNext();?)?{ ??
  • ????????HSSFRow?row?=?rit.next(); ??
  • ????????for?(Iterator<HSSFCell>?cit?=?(Iterator<HSSFCell>)row.cellIterator();?cit.hasNext();?)?{ ??
  • ????????????HSSFCell?cell?=?cit.next(); ??
  • ????????????//?Do?something?here ??
  • ????????} ??
  • ????}??
  • Sheet sheet = wb.getSheetAt(0);for (Iterator rit = sheet.rowIterator(); rit.hasNext(); ) {Row row = (Row)rit.next();for (Iterator cit = row.cellIterator(); cit.hasNext(); ) {Cell cell = (Cell)cit.next();// Do something here}}HSSFSheet sheet = wb.getSheetAt(0);for (Iterator<HSSFRow> rit = (Iterator<HSSFRow>)sheet.rowIterator(); rit.hasNext(); ) {HSSFRow row = rit.next();for (Iterator<HSSFCell> cit = (Iterator<HSSFCell>)row.cellIterator(); cit.hasNext(); ) {HSSFCell cell = cit.next();// Do something here}}

    ?

    java高級for循環(huán)迭代行和單元格

    ?

    Java代碼 ?
  • Sheet?sheet?=?wb.getSheetAt(0); ??
  • ????for?(Row?row?:?sheet)?{ ??
  • ????????for?(Cell?cell?:?row)?{ ??
  • ????????????//?Do?something?here ??
  • ????????} ??
  • ????}??
  • Sheet sheet = wb.getSheetAt(0);for (Row row : sheet) {for (Cell cell : row) {// Do something here}}

    ?

    9.? 得到單元格的內(nèi)容

    ?

    ???? 想得到單元格的內(nèi)容之前,首先要知道單元格的類型,因此你要先判斷單元格的類型之后選擇合適的方法得到單元格的值。下面的代碼,循環(huán)得到一個Sheet所有的單元格。

    ?

    Java代碼 ?
  • public?void?getCellValue()?throws?Exception?{ ??
  • ????????InputStream?inp?=?new?FileInputStream("D:\\hjn.xls"); ??
  • ????????HSSFWorkbook?wb?=?new?HSSFWorkbook(new?POIFSFileSystem(inp)); ??
  • ????????Sheet?sheet1?=?wb.getSheetAt(0); ??
  • ????????for?(Row?row?:?sheet1)?{ ??
  • ????????????for?(Cell?cell?:?row)?{ ??
  • ????????????????CellReference?cellRef?=?new?CellReference(row.getRowNum(),?cell ??
  • ????????????????????????.getColumnIndex()); ??
  • ????????????????System.out.print(cellRef.formatAsString()); ??
  • ????????????????System.out.print("?-?"); ??
  • ??
  • ????????????????switch?(cell.getCellType())?{ ??
  • ????????????????case?Cell.CELL_TYPE_STRING: ??
  • ????????????????????System.out.println(cell.getRichStringCellValue() ??
  • ????????????????????????????.getString()); ??
  • ????????????????????break; ??
  • ????????????????case?Cell.CELL_TYPE_NUMERIC: ??
  • ????????????????????if?(DateUtil.isCellDateFormatted(cell))?{ ??
  • ????????????????????????System.out.println(cell.getDateCellValue()); ??
  • ????????????????????}?else?{ ??
  • ????????????????????????System.out.println(cell.getNumericCellValue()); ??
  • ????????????????????} ??
  • ????????????????????break; ??
  • ????????????????case?Cell.CELL_TYPE_BOOLEAN: ??
  • ????????????????????System.out.println(cell.getBooleanCellValue()); ??
  • ????????????????????break; ??
  • ????????????????case?Cell.CELL_TYPE_FORMULA: ??
  • ????????????????????System.out.println(cell.getCellFormula()); ??
  • ????????????????????break; ??
  • ????????????????default: ??
  • ????????????????????System.out.println(); ??
  • ????????????????} ??
  • ????????????} ??
  • ????????} ??
  • ??
  • ????}??
  • public void getCellValue() throws Exception {InputStream inp = new FileInputStream("D:\\hjn.xls");HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));Sheet sheet1 = wb.getSheetAt(0);for (Row row : sheet1) {for (Cell cell : row) {CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());System.out.print(cellRef.formatAsString());System.out.print(" - ");switch (cell.getCellType()) {case Cell.CELL_TYPE_STRING:System.out.println(cell.getRichStringCellValue().getString());break;case Cell.CELL_TYPE_NUMERIC:if (DateUtil.isCellDateFormatted(cell)) {System.out.println(cell.getDateCellValue());} else {System.out.println(cell.getNumericCellValue());}break;case Cell.CELL_TYPE_BOOLEAN:System.out.println(cell.getBooleanCellValue());break;case Cell.CELL_TYPE_FORMULA:System.out.println(cell.getCellFormula());break;default:System.out.println();}}}}

    ?

    10. 文本提取

    ?

    poi的ExcelExtractor可以抽取Cell中的值。org.apache.poi.ss.extractor 為抽取類的接口,ExcelExtractor, XSSFExcelExtractor實現(xiàn)了該接口。

    ?

    Java代碼 ?
  • InputStream?inp?=?new?FileInputStream("D:\\hjn.xls"); ??
  • ????????HSSFWorkbook??wb?=?new?HSSFWorkbook(new?POIFSFileSystem(inp)); ??
  • ???????? ??
  • ????????ExcelExtractor?extractor?=?new?ExcelExtractor(wb); ??
  • ??
  • ????????extractor.setFormulasNotResults(true); ??
  • ????????extractor.setIncludeSheetNames(true); ??
  • ????????String?text?=?extractor.getText(); ??
  • ????????System.out.println(text);??
  • InputStream inp = new FileInputStream("D:\\hjn.xls");HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));ExcelExtractor extractor = new ExcelExtractor(wb);extractor.setFormulasNotResults(true);extractor.setIncludeSheetNames(true);String text = extractor.getText();System.out.println(text);

    ?

    ?11. 填充和顏色

    ?

    Java代碼 ?
  • public?void?fillAndColors()?throws?Exception{ ??
  • ????????Workbook?wb?=?new?HSSFWorkbook(); ??
  • ????????Sheet?sheet?=?wb.createSheet("new?sheet"); ??
  • ??
  • ????????//?Create?a?row?and?put?some?cells?in?it.?Rows?are?0?based. ??
  • ????????Row?row?=?sheet.createRow((short)?1); ??
  • ??
  • ????????//?Aqua?background ??
  • ????????CellStyle?style?=?wb.createCellStyle(); ??
  • ????????style.setFillBackgroundColor(IndexedColors.BLUE.getIndex()); ??
  • ????????style.setFillPattern(CellStyle.ALIGN_FILL); ??
  • ????????Cell?cell?=?row.createCell((short)?1); ??
  • ????????cell.setCellValue("X"); ??
  • ????????cell.setCellStyle(style); ??
  • ??
  • ????????//?Orange?"foreground",?foreground?being?the?fill?foreground?not?the?font?color. ??
  • ????????style?=?wb.createCellStyle(); ??
  • ????????style.setFillForegroundColor(IndexedColors.ORANGE.getIndex()); ??
  • ????????style.setFillPattern(CellStyle.SOLID_FOREGROUND); ??
  • ????????cell?=?row.createCell((short)?2); ??
  • ????????cell.setCellValue("X"); ??
  • ????????cell.setCellStyle(style); ??
  • ??
  • ????????//?Write?the?output?to?a?file ??
  • ????????FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • ????????wb.write(fileOut); ??
  • ????????fileOut.close(); ??
  • ??
  • ????}??
  • public void fillAndColors() throws Exception{Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("new sheet");// Create a row and put some cells in it. Rows are 0 based.Row row = sheet.createRow((short) 1);// Aqua backgroundCellStyle style = wb.createCellStyle();style.setFillBackgroundColor(IndexedColors.BLUE.getIndex());style.setFillPattern(CellStyle.ALIGN_FILL);Cell cell = row.createCell((short) 1);cell.setCellValue("X");cell.setCellStyle(style);// Orange "foreground", foreground being the fill foreground not the font color.style = wb.createCellStyle();style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row.createCell((short) 2);cell.setCellValue("X");cell.setCellStyle(style);// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}

    ?

    12. 合并單元格

    ?

    Java代碼 ?
  • public?void?mergingCell()?throws?Exception{ ??
  • ????????Workbook?wb?=?new?HSSFWorkbook(); ??
  • ????????Sheet?sheet?=?wb.createSheet("new?sheet"); ??
  • ????????Row?row?=?sheet.createRow((short)?1); ??
  • ????????Cell?cell?=?row.createCell((short)?1); ??
  • ????????cell.setCellValue("This?is?a?test?of?merging"); ??
  • ??
  • ????????sheet.addMergedRegion(new?CellRangeAddress(1,?//?first?row?(0-based) ??
  • ????????????????4,?//?last?row?(0-based) ??
  • ????????????????1,?//?first?column?(0-based) ??
  • ????????????????6?//?last?column?(0-based) ??
  • ????????????????)); ??
  • ??
  • ????????//?Write?the?output?to?a?file ??
  • ????????FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • ????????wb.write(fileOut); ??
  • ????????fileOut.close(); ??
  • ??
  • ????}??
  • public void mergingCell() throws Exception{Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("new sheet");Row row = sheet.createRow((short) 1);Cell cell = row.createCell((short) 1);cell.setCellValue("This is a test of merging");sheet.addMergedRegion(new CellRangeAddress(1, // first row (0-based)4, // last row (0-based)1, // first column (0-based)6 // last column (0-based)));// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}

    ?

    13. 設(shè)置字體

    ?

    Java代碼 ?
  • public?void?createFont()?throws?Exception{ ??
  • ????????Workbook?wb?=?new?HSSFWorkbook(); ??
  • ????????Sheet?sheet?=?wb.createSheet("new?sheet"); ??
  • ??
  • ????????//?Create?a?row?and?put?some?cells?in?it.?Rows?are?0?based. ??
  • ????????Row?row?=?sheet.createRow(1); ??
  • ??
  • ????????//?Create?a?new?font?and?alter?it. ??
  • ????????Font?font?=?wb.createFont(); ??
  • ????????font.setFontHeightInPoints((short)24); ??
  • ????????font.setFontName("Courier?New"); ??
  • ????????font.setItalic(true); ??
  • ????????font.setStrikeout(true); ??
  • ??
  • ????????//?Fonts?are?set?into?a?style?so?create?a?new?one?to?use. ??
  • ????????CellStyle?style?=?wb.createCellStyle(); ??
  • ????????style.setFont(font); ??
  • ??
  • ????????//?Create?a?cell?and?put?a?value?in?it. ??
  • ????????Cell?cell?=?row.createCell(1); ??
  • ????????cell.setCellValue("This?is?a?test?of?fonts"); ??
  • ????????cell.setCellStyle(style); ??
  • ??
  • ????????//?Write?the?output?to?a?file ??
  • ????????FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • ????????wb.write(fileOut); ??
  • ????????fileOut.close(); ??
  • ??
  • ????}??
  • public void createFont() throws Exception{Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("new sheet");// Create a row and put some cells in it. Rows are 0 based.Row row = sheet.createRow(1);// Create a new font and alter it.Font font = wb.createFont();font.setFontHeightInPoints((short)24);font.setFontName("Courier New");font.setItalic(true);font.setStrikeout(true);// Fonts are set into a style so create a new one to use.CellStyle style = wb.createCellStyle();style.setFont(font);// Create a cell and put a value in it.Cell cell = row.createCell(1);cell.setCellValue("This is a test of fonts");cell.setCellStyle(style);// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}

    ?

    注意:一個工作薄最多只能創(chuàng)建32767 個不同的字體樣式,因此你應(yīng)該重用字體樣式而不應(yīng)該每創(chuàng)建一個單元格就創(chuàng)建一個單元格字體樣式。

    錯誤寫法:

    ?

    Java代碼 ?
  • for?(int?i?=?0;?i?<?10000;?i++)?{ ??
  • ??????????Row?row?=?sheet.createRow(i); ??
  • ??????????Cell?cell?=?row.createCell((short)?0); ??
  • ??
  • ??????????CellStyle?style?=?workbook.createCellStyle(); ??
  • ??????????Font?font?=?workbook.createFont(); ??
  • ??????????font.setBoldweight(Font.BOLDWEIGHT_BOLD); ??
  • ??????????style.setFont(font); ??
  • ??????????cell.setCellStyle(style); ??
  • ??????}??
  • for (int i = 0; i < 10000; i++) {Row row = sheet.createRow(i);Cell cell = row.createCell((short) 0);CellStyle style = workbook.createCellStyle();Font font = workbook.createFont();font.setBoldweight(Font.BOLDWEIGHT_BOLD);style.setFont(font);cell.setCellStyle(style);}

    ?
    正確寫法:

    ?

    Java代碼 CellStyle?style?=?workbook.createCellStyle(); ??
  • Font?font?=?workbook.createFont(); ??
  • font.setBoldweight(Font.BOLDWEIGHT_BOLD); ??
  • style.setFont(font); ??
  • for?(int?i?=?0;?i?<?10000;?i++)?{ ??
  • ????Row?row?=?sheet.createRow(i); ??
  • ????Cell?cell?=?row.createCell((short)?0); ??
  • ????cell.setCellStyle(style); ??
  • }??
  • ?

    14. 自定義顏色

    ????? HSSF:

    ?

    Java代碼 ?
  • HSSFWorkbook?wb?=?new?HSSFWorkbook(); ??
  • ????HSSFSheet?sheet?=?wb.createSheet(); ??
  • ????HSSFRow?row?=?sheet.createRow((short)?0); ??
  • ????HSSFCell?cell?=?row.createCell((short)?0); ??
  • ????cell.setCellValue("Default?Palette"); ??
  • ??
  • ????//apply?some?colors?from?the?standard?palette, ??
  • ????//?as?in?the?previous?examples. ??
  • ????//we'll?use?red?text?on?a?lime?background ??
  • ??
  • ????HSSFCellStyle?style?=?wb.createCellStyle(); ??
  • ????style.setFillForegroundColor(HSSFColor.LIME.index); ??
  • ????style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); ??
  • ??
  • ????HSSFFont?font?=?wb.createFont(); ??
  • ????font.setColor(HSSFColor.RED.index); ??
  • ????style.setFont(font); ??
  • ??
  • ????cell.setCellStyle(style); ??
  • ??
  • ????//save?with?the?default?palette ??
  • ????FileOutputStream?out?=?new?FileOutputStream("default_palette.xls"); ??
  • ????wb.write(out); ??
  • ????out.close(); ??
  • ??
  • ????//now,?let's?replace?RED?and?LIME?in?the?palette ??
  • ????//?with?a?more?attractive?combination ??
  • ????//?(lovingly?borrowed?from?freebsd.org) ??
  • ??
  • ????cell.setCellValue("Modified?Palette"); ??
  • ??
  • ????//creating?a?custom?palette?for?the?workbook ??
  • ????HSSFPalette?palette?=?wb.getCustomPalette(); ??
  • ??
  • ????//replacing?the?standard?red?with?freebsd.org?red ??
  • ????palette.setColorAtIndex(HSSFColor.RED.index, ??
  • ????????????(byte)?153,??//RGB?red?(0-255) ??
  • ????????????(byte)?0,????//RGB?green ??
  • ????????????(byte)?0?????//RGB?blue ??
  • ????); ??
  • ????//replacing?lime?with?freebsd.org?gold ??
  • ????palette.setColorAtIndex(HSSFColor.LIME.index,?(byte)?255,?(byte)?204,?(byte)?102); ??
  • ??
  • ????//save?with?the?modified?palette ??
  • ????//?note?that?wherever?we?have?previously?used?RED?or?LIME,?the ??
  • ????//?new?colors?magically?appear ??
  • ????out?=?new?FileOutputStream("modified_palette.xls"); ??
  • ????wb.write(out); ??
  • ????out.close();??
  • HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet();HSSFRow row = sheet.createRow((short) 0);HSSFCell cell = row.createCell((short) 0);cell.setCellValue("Default Palette");//apply some colors from the standard palette,// as in the previous examples.//we'll use red text on a lime backgroundHSSFCellStyle style = wb.createCellStyle();style.setFillForegroundColor(HSSFColor.LIME.index);style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);HSSFFont font = wb.createFont();font.setColor(HSSFColor.RED.index);style.setFont(font);cell.setCellStyle(style);//save with the default paletteFileOutputStream out = new FileOutputStream("default_palette.xls");wb.write(out);out.close();//now, let's replace RED and LIME in the palette// with a more attractive combination// (lovingly borrowed from freebsd.org)cell.setCellValue("Modified Palette");//creating a custom palette for the workbookHSSFPalette palette = wb.getCustomPalette();//replacing the standard red with freebsd.org redpalette.setColorAtIndex(HSSFColor.RED.index,(byte) 153, //RGB red (0-255)(byte) 0, //RGB green(byte) 0 //RGB blue);//replacing lime with freebsd.org goldpalette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);//save with the modified palette// note that wherever we have previously used RED or LIME, the// new colors magically appearout = new FileOutputStream("modified_palette.xls");wb.write(out);out.close();

    ?

    XSSF:

    ?

    Java代碼 ?
  • XSSFWorkbook?wb?=?new?XSSFWorkbook(); ??
  • ????XSSFSheet?sheet?=?wb.createSheet(); ??
  • ????XSSFRow?row?=?sheet.createRow(0); ??
  • ????XSSFCell?cell?=?row.createCell(?0); ??
  • ????cell.setCellValue("custom?XSSF?colors"); ??
  • ??
  • ????XSSFCellStyle?style1?=?wb.createCellStyle(); ??
  • ????style1.setFillForegroundColor(new?XSSFColor(new?java.awt.Color(128,?0,?128))); ??
  • ????style1.setFillPattern(CellStyle.SOLID_FOREGROUND);??
  • XSSFWorkbook wb = new XSSFWorkbook();XSSFSheet sheet = wb.createSheet();XSSFRow row = sheet.createRow(0);XSSFCell cell = row.createCell( 0);cell.setCellValue("custom XSSF colors");XSSFCellStyle style1 = wb.createCellStyle();style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128)));style1.setFillPattern(CellStyle.SOLID_FOREGROUND);

    ?

    ?

    15. 讀取和重寫工作薄

    ?

    Java代碼 ?
  • InputStream?inp?=?new?FileInputStream("workbook.xls"); ??
  • ????//InputStream?inp?=?new?FileInputStream("workbook.xlsx"); ??
  • ??
  • ????Workbook?wb?=?WorkbookFactory.create(inp); ??
  • ????Sheet?sheet?=?wb.getSheetAt(0); ??
  • ????Row?row?=?sheet.getRow(2); ??
  • ????Cell?cell?=?row.getCell(3); ??
  • ????if?(cell?==?null) ??
  • ????????cell?=?row.createCell(3); ??
  • ????cell.setCellType(Cell.CELL_TYPE_STRING); ??
  • ????cell.setCellValue("a?test"); ??
  • ??
  • ????//?Write?the?output?to?a?file ??
  • ????FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • ????wb.write(fileOut); ??
  • ????fileOut.close(); ??
  • ??????????????????????
  • InputStream inp = new FileInputStream("workbook.xls");//InputStream inp = new FileInputStream("workbook.xlsx");Workbook wb = WorkbookFactory.create(inp);Sheet sheet = wb.getSheetAt(0);Row row = sheet.getRow(2);Cell cell = row.getCell(3);if (cell == null)cell = row.createCell(3);cell.setCellType(Cell.CELL_TYPE_STRING);cell.setCellValue("a test");// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();

    ?

    ?

    16. 單元格內(nèi)容換行

    ?

    Java代碼 ?
  • Workbook?wb?=?new?XSSFWorkbook();???//or?new?HSSFWorkbook(); ??
  • ????Sheet?sheet?=?wb.createSheet(); ??
  • ??
  • ????Row?row?=?sheet.createRow(2); ??
  • ????Cell?cell?=?row.createCell(2); ??
  • ????cell.setCellValue("Use?\n?with?word?wrap?on?to?create?a?new?line"); ??
  • ??
  • ????//to?enable?newlines?you?need?set?a?cell?styles?with?wrap=true ??
  • ????CellStyle?cs?=?wb.createCellStyle(); ??
  • ????cs.setWrapText(true); ??
  • ????cell.setCellStyle(cs); ??
  • ??
  • ????//increase?row?height?to?accomodate?two?lines?of?text ??
  • ????row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints())); ??
  • ??
  • ????//adjust?column?width?to?fit?the?content ??
  • ????sheet.autoSizeColumn((short)2); ??
  • ??
  • ????FileOutputStream?fileOut?=?new?FileOutputStream("ooxml-newlines.xlsx"); ??
  • ????wb.write(fileOut); ??
  • ????fileOut.close();??
  • Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();Sheet sheet = wb.createSheet();Row row = sheet.createRow(2);Cell cell = row.createCell(2);cell.setCellValue("Use \n with word wrap on to create a new line");//to enable newlines you need set a cell styles with wrap=trueCellStyle cs = wb.createCellStyle();cs.setWrapText(true);cell.setCellStyle(cs);//increase row height to accomodate two lines of textrow.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));//adjust column width to fit the contentsheet.autoSizeColumn((short)2);FileOutputStream fileOut = new FileOutputStream("ooxml-newlines.xlsx");wb.write(fileOut);fileOut.close();

    ?

    ?換行的步驟:

    • 在需要換行的地方加上\n?? cell.setCellValue("Use \n with word wrap on to create a new line");
    • 設(shè)置行高 row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));
    • 設(shè)置列只適應(yīng)寬度 sheet.autoSizeColumn((short)2);

    ?17. 數(shù)據(jù)格式化

    ?

    Java代碼 ?
  • public?void?dataFormat()?throws?Exception{ ??
  • ????????Workbook?wb?=?new?HSSFWorkbook(); ??
  • ????????Sheet?sheet?=?wb.createSheet("format?sheet"); ??
  • ????????CellStyle?style; ??
  • ????????DataFormat?format?=?wb.createDataFormat(); ??
  • ????????Row?row; ??
  • ????????Cell?cell; ??
  • ????????short?rowNum?=?0; ??
  • ????????short?colNum?=?0; ??
  • ??
  • ????????row?=?sheet.createRow(rowNum++); ??
  • ????????cell?=?row.createCell(colNum); ??
  • ????????cell.setCellValue(11111.25); ??
  • ????????style?=?wb.createCellStyle(); ??
  • ????????style.setDataFormat(format.getFormat("0.0")); ??
  • ????????cell.setCellStyle(style); ??
  • ??
  • ????????row?=?sheet.createRow(rowNum++); ??
  • ????????cell?=?row.createCell(colNum); ??
  • ????????cell.setCellValue(1111.25); ??
  • ????????style?=?wb.createCellStyle(); ??
  • ????????style.setDataFormat(format.getFormat("#,###.0000")); ??
  • ????????cell.setCellStyle(style); ??
  • ??
  • ????????FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • ????????wb.write(fileOut); ??
  • ????????fileOut.close(); ??
  • ??
  • ????}??
  • public void dataFormat() throws Exception{Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("format sheet");CellStyle style;DataFormat format = wb.createDataFormat();Row row;Cell cell;short rowNum = 0;short colNum = 0;row = sheet.createRow(rowNum++);cell = row.createCell(colNum);cell.setCellValue(11111.25);style = wb.createCellStyle();style.setDataFormat(format.getFormat("0.0"));cell.setCellStyle(style);row = sheet.createRow(rowNum++);cell = row.createCell(colNum);cell.setCellValue(1111.25);style = wb.createCellStyle();style.setDataFormat(format.getFormat("#,###.0000"));cell.setCellStyle(style);FileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}

    ?

    18. 設(shè)置打印區(qū)域

    ?

    Java代碼 ?
  • Workbook?wb?=?new?HSSFWorkbook(); ??
  • ????Sheet?sheet?=?wb.createSheet("Sheet1"); ??
  • ????//sets?the?print?area?for?the?first?sheet ??
  • ????wb.setPrintArea(0,?"$A$1:$C$2"); ??
  • ???? ??
  • ????//Alternatively: ??
  • ????wb.setPrintArea( ??
  • ????????????0,?//sheet?index ??
  • ????????????0,?//start?column ??
  • ????????????1,?//end?column ??
  • ????????????0,?//start?row ??
  • ????????????0??//end?row ??
  • ????); ??
  • ??
  • ????FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • ????wb.write(fileOut); ??
  • ????fileOut.close();??
  • Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("Sheet1");//sets the print area for the first sheetwb.setPrintArea(0, "$A$1:$C$2");//Alternatively:wb.setPrintArea(0, //sheet index0, //start column1, //end column0, //start row0 //end row);FileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();

    ?

    19. 設(shè)置頁腳

    ?

    ???

    Java代碼 ?
  • HSSFWorkbook?wb?=?new?HSSFWorkbook(); ??
  • ????HSSFSheet?sheet?=?wb.createSheet("format?sheet"); ??
  • ????HSSFFooter?footer?=?sheet.getFooter(); ??
  • ??
  • ????footer.setRight(?"Page?"?+?HSSFFooter.page()?+?"?of?"?+?HSSFFooter.numPages()?); ??
  • ??
  • ??
  • ??
  • ????//?Create?various?cells?and?rows?for?spreadsheet. ??
  • ??
  • ????FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • ????wb.write(fileOut); ??
  • ????fileOut.close();??
  • HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("format sheet");HSSFFooter footer = sheet.getFooter();footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );// Create various cells and rows for spreadsheet.FileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();

    ?

    ? 備注:只有在打印預(yù)覽的時候頁腳才顯示出來。

    ?20. 選中一個Sheet

    Java代碼 ?
  • Workbook?wb?=?new?HSSFWorkbook(); ??
  • ??Sheet?sheet?=?wb.createSheet("row?sheet"); ??
  • ??sheet.setSelected(true);??
  • Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("row sheet");sheet.setSelected(true);

    ?

    ?21. 設(shè)置方法倍率

    Java代碼 ?
  • Workbook?wb?=?new?HSSFWorkbook(); ??
  • ???Sheet?sheet1?=?wb.createSheet("new?sheet"); ??
  • ???sheet1.setZoom(3,4);???//?75?percent?magnification??
  • Workbook wb = new HSSFWorkbook();Sheet sheet1 = wb.createSheet("new sheet");sheet1.setZoom(3,4); // 75 percent magnification

    ?Zoom是一個分數(shù),例如如果方法75%,那么分子為3,分母為4。

    ?

    ? 22. 設(shè)置打印時的頁寬和頁高

    ?

    Java代碼 ?
  • Workbook?wb?=?new?HSSFWorkbook(); ??
  • ????Sheet?sheet?=?wb.createSheet("format?sheet"); ??
  • ????PrintSetup?ps?=?sheet.getPrintSetup(); ??
  • ??
  • ????sheet.setAutobreaks(true); ??
  • ??
  • ????ps.setFitHeight((short)1); ??
  • ????ps.setFitWidth((short)1); ??
  • ??
  • ??
  • ????//?Create?various?cells?and?rows?for?spreadsheet. ??
  • ??
  • ????FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • ????wb.write(fileOut); ??
  • ????fileOut.close();??
  • Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("format sheet");PrintSetup ps = sheet.getPrintSetup();sheet.setAutobreaks(true);ps.setFitHeight((short)1);ps.setFitWidth((short)1);// Create various cells and rows for spreadsheet.FileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();

    ?

    23. 移動行

    ?

    Java代碼 ?
  • public?void?shiftRow()?throws?Exception{ ??
  • ????????Workbook?wb?=?new?HSSFWorkbook(); ??
  • ????????Sheet?sheet?=?wb.createSheet("row?sheet"); ??
  • ??
  • ????????for(int?i=0;i<20;i++){ ??
  • ????????????Row?row=sheet.createRow(i); ??
  • ????????????Cell?cell=row.createCell(0); ??
  • ????????????cell.setCellValue(""+i); ??
  • ????????} ??
  • ????????//?Shift?rows?6?-?11?on?the?spreadsheet?to?the?top?(rows?0?-?5) ??
  • ????????//把第6-11行向上移動5行 ??
  • ????????sheet.shiftRows(5,?10,-5); ??
  • ????????FileOutputStream?fileOut?=?new?FileOutputStream("workbook.xls"); ??
  • ????????wb.write(fileOut); ??
  • ????????fileOut.close(); ??
  • ????}??
  • public void shiftRow() throws Exception{Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("row sheet");for(int i=0;i<20;i++){Row row=sheet.createRow(i);Cell cell=row.createCell(0);cell.setCellValue(""+i);}// Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)//把第6-11行向上移動5行sheet.shiftRows(5, 10,-5);FileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();}

    ?POI學習筆記四將會介紹poi更高級的一些知識點。

    ?

    總結(jié)

    以上是生活随笔為你收集整理的POI学习笔记 自定义颜色的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。