有須要可點贊收藏,Tips:javascript
- 本文 api 適合 xls 格式Excel
- 本文非原創,爲了配合圖文教程纔拿出來
HSSFWorkbook excel文檔對象
HSSFSheet excel的sheet
HSSFRow excel的行
HSSFCell excel的單元格
HSSFFont excel字體
HSSFName 名稱
HSSFDataFormat 日期格式
HSSFHeader sheet頭
HSSFFooter sheet尾
HSSFCellStyle cell樣式
HSSFDateUtil 日期
HSSFPrintSetup 打印
HSSFErrorConstants 錯誤信息表
複製代碼
POIFSFileSystem fs=newPOIFSFileSystem(new FileInputStream("d:/test.xls"));
//獲得Excel工做簿對象
HSSFWorkbook wb = new HSSFWorkbook(fs);
//獲得Excel工做表對象
HSSFSheet sheet = wb.getSheetAt(0);
//獲得Excel工做表的行
HSSFRow row = sheet.getRow(i);
//獲得Excel工做表指定行的單元格
HSSFCell cell = row.getCell((short) j);
//獲得單元格樣式
cellStyle = cell.getCellStyle();
複製代碼
//建立Excel工做簿對象
HSSFWorkbook wb = new HSSFWorkbook();
//建立Excel工做表對象
HSSFSheet sheet = wb.createSheet("new sheet");
//建立Excel工做表的行 下標從0開始
HSSFRow row = sheet.createRow(0);
//建立單元格樣式
cellStyle = wb.createCellStyle();
//建立單元格
HSSFCell cell =row.createCell(0);
//爲單元格設置樣式 下標從0開始
cell.setCellStyle(cellStyle);
//爲單元格設置值
cell.setCellValue(1);
複製代碼
wb.setSheetName(1, "第一張工做表",HSSFCell.ENCODING_UTF_16);
cell.setEncoding((short) 1);
cell.setCellValue("單元格內容");
複製代碼
wb.getNumberOfSheets()
複製代碼
HSSFSheet sheet = wb.getSheetAt(0);
複製代碼
int rowcount = sheet.getLastRowNum();
複製代碼
row.getLastCellNum();
複製代碼
//設置單元格爲STRING類型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//讀取爲數值類型的單元格內容
cell.getNumericCellValue();
複製代碼
sheet.setColumnWidth((short)column,(short)width);
row.setHeight((short)height);
複製代碼
//合併從第rowFrom行columnFrom列
Region region = new Region((short)rowFrom,(short)columnFrom,(short)rowTo
,(short)columnTo);
// 到rowTo行columnTo的區域
sheet.addMergedRegion(region);
//獲得全部區域
sheet.getNumMergedRegions() 12345
複製代碼
FileOutputStream fileOut = new FileOutputStream(path);
wb.write(fileOut);
複製代碼
public String getCellStringValue(HSSFCell cell) {
String cellValue = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING://字符串類型
cellValue = cell.getStringCellValue();
if(cellValue.trim().equals("")||cellValue.trim().length()<=0)
cellValue=" ";
break;
case HSSFCell.CELL_TYPE_NUMERIC: //數值類型
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA: //公式
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
cellValue=" ";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
break;
case HSSFCell.CELL_TYPE_ERROR:
break;
default:
break;
}
return cellValue;
}
複製代碼
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_DOTTED);//下邊框
style.setBorderLeft(HSSFCellStyle.BORDER_DOTTED);//左邊框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框
複製代碼
HSSFFont f = wb.createFont();
f.setFontHeightInPoints((short) 11);//字號
f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗
style.setFont(f);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
style.setRotation(short rotation);//單元格內容的旋轉的角度
HSSFDataFormat df = wb.createDataFormat();
style1.setDataFormat(df.getFormat("0.00%"));//設置單元格數據格式
cell.setCellFormula(string);//給單元格設公式
style.setRotation(short rotation);//單元格內容的旋轉的角度
複製代碼
//先把讀進來的圖片放到一個ByteArrayOutputStream中,以便產生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));
ImageIO.write(bufferImg,"jpg",byteArrayOut);
//讀進一個excel模版
FileInputStream fos = new FileInputStream(filePathName+"/stencil.xlt");
fs = new POIFSFileSystem(fos);
//建立一個工做薄
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1023,255,(short) 0,0,(short)10,10);
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
複製代碼
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("format sheet");
HSSFPrintSetup ps = sheet.getPrintSetup();
sheet.setAutobreaks(true);
ps.setFitHeight((short)1);
ps.setFitWidth((short)1);
複製代碼
HSSFSheet sheet = wb.createSheet("Sheet1");
wb.setPrintArea(0, "$A$1:$C$2");
複製代碼
HSSFSheet sheet = wb.createSheet("format sheet");
HSSFFooter footer = sheet.getFooter()
footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
複製代碼
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("row sheet");
// Create various cells and rows for spreadsheet.
// Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
sheet.shiftRows(5, 10, -5);
複製代碼
HSSFSheet sheet = wb.createSheet("row sheet");
heet.setSelected(true);
複製代碼
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.setZoom(1,2); // 50 percent magnification
複製代碼
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFHeader header = sheet.getHeader();
header.setCenter("Center Header");
header.setLeft("Left Header");
header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16"); 123456
複製代碼
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);
複製代碼
HSSFCellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(HSSFColor.AQUA.index);
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("X");
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cell.setCellStyle(style);
複製代碼
HSSFFormulaEvaluator eval=new HSSFFormulaEvaluator((HSSFWorkbook) wb);
private static void updateFormula(Workbook wb,Sheet s,int row){
Row r=s.getRow(row);
Cell c=null;
FormulaEcaluator eval=null;
if(wb instanceof HSSFWorkbook)
eval=new HSSFFormulaEvaluator((HSSFWorkbook) wb);
else if(wb instanceof XSSFWorkbook)
eval=new XSSFFormulaEvaluator((XSSFWorkbook) wb);
for(int i=r.getFirstCellNum();i
c=r.getCell(i);
if(c.getCellType()==Cell.CELL_TYPE_FORMULA)
eval.evaluateFormulaCell(c);
}
}
複製代碼
說明:FormulaEvaluator提供了evaluateFormulaCell(Cell cell)方法,計算公式保存結果,但不改變公式。而evaluateInCell(Cell cell) 方法是計算公式,並將原公式替換爲計算結果,也就是說該單元格的類型不在是Cell.CELL_TYPE_FORMULA而是Cell.CELL_TYPE_NUMBERIC。HSSFFormulaEvaluator提供了靜態方法evaluateAllFormulaCells(HSSFWorkbook wb) ,計算一個Excel文件的全部公式,用起來很方便。css
sheet.setDisplayGridlines(false);其中sheet是Sheet對象
複製代碼
cellStyle.setWrapText(true);//其中cellStyle是WorkBook建立的CellStyle對象,而後將cellStyle設置到要換行的Cell對象,最後在要換行的對象(通常爲字符串)加入」/r/n」。如 topTile.append(「/r/n」 +」cellContent」);
複製代碼
sheet.addMergedRegion(new CellRangeAddress(0, 4, 0, 2));本示例爲合併4行2列
複製代碼
HSSFHeader header = sheet.getHeader();
header.setCenter(「Center Header」);
header.setLeft(「Left Header」);
header.setRight(HSSFHeader.font(「Stencil-Normal」, 「Italic」) +
HSSFHeader.fontSize((short) 16) + 「Right w/ Stencil-Normal Italic font and size 16」);
HSSFFooter footer = (HSSFFooter )sheet.getFooter()
footer.setRight( 「Page 」 + HSSFFooter.page() + 」 of 」 + HSSFFooter.numPages() );
複製代碼
HSSFPrintSetup print = (HSSFPrintSetup) sheet.getPrintSetup();
print.setLandscape(true);//設置橫向打印
print.setScale((short) 70);//設置打印縮放70%
print.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);//設置爲A4紙張
print.setLeftToRight(true);//設置打印順序先行後列,默認爲先列行
print.setFitHeight((short) 10);設置縮放調整爲10頁高
print.setFitWidth((short) 10);設置縮放調整爲寬高
sheet.setAutobreaks(false);
if (i != 0 && i % 30 == 0)
sheet.setRowBreak(i);//設置每30行分頁打印
複製代碼
HSSFWorkbook wb = new HSSFWorkbook();
wb.setRepeatingRowsAndColumns(0, 0, 12, 1, 6);//設置1到12列,行1到6每一頁重複打印
複製代碼
sheet.setAutobreaks(true);
sheet.setColumnWidth((short)i,colsWidth[i]); //設定單元格長度
sheet.autoSizeColumn((short) i);//自動根據長度調整單元格長度
複製代碼