1、 得到Excel常用对象
view plaincopy to clipboardprint?
01.POIFSFileSystem fs=newPOIFSFileSystem(new FileInputStream("d:/test.xls"));
02.//得到Excel工作簿对象
03.HSSFWorkbook wb = new HSSFWorkbook(fs);
04.//得到Excel工作表对象
05.HSSFSheet sheet = wb.getSheetAt(0);
06.//得到Excel工作表的行
07.HSSFRow row = sheet.getRow(i);
08.//得到Excel工作表指定行的单元格
09.HSSFCell cell = row.getCell((short) j);
10.cellStyle = cell.getCellStyle();//得到单元格样式
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();//得到单元格样式
2、建立Excel常用对象view plaincopy to clipboardprint?
01.HSSFWorkbook wb = new HSSFWorkbook();//创建Excel工作簿对象
02.HSSFSheet sheet = wb.createSheet("new sheet");//创建Excel工作表对象
03.HSSFRow row = sheet.createRow((short)0); //创建Excel工作表的行
04.cellStyle = wb.createCellStyle();//创建单元格样式
05.row.createCell((short)0).setCellStyle(cellStyle); //创建Excel工作表指定行的单元格
06.row.createCell((short)0).setCellValue(1); //设置Excel工作表的值
HSSFWorkbook wb = new HSSFWorkbook();//创建Excel工作簿对象
HSSFSheet sheet = wb.createSheet("new sheet");//创建Excel工作表对象
HSSFRow row = sheet.createRow((short)0); //创建Excel工作表的行
cellStyle = wb.createCellStyle();//创建单元格样式
row.createCell((short)0).setCellStyle(cellStyle); //创建Excel工作表指定行的单元格
row.createCell((short)0).setCellValue(1); //设置Excel工作表的值
3、设置sheet名称和单元格内容view plaincopy to clipboardprint?
01.wb.setSheetName(1, "第一张工作表",HSSFCell.ENCODING_UTF_16);
02.cell.setEncoding((short) 1);
03.cell.setCellValue("单元格内容");
wb.setSheetName(1, "第一张工作表",HSSFCell.ENCODING_UTF_16);
cell.setEncoding((short) 1);
cell.setCellValue("单元格内容");
4、取得sheet的数目
view plaincopy to clipboardprint?
01.wb.getNumberOfSheets()
wb.getNumberOfSheets()
5、 根据index取得sheet对象view plaincopy to clipboardprint?
01.HSSFSheet sheet = wb.getSheetAt(0);
HSSFSheet sheet = wb.getSheetAt(0);
6、取得有效的行数view plaincopy to clipboardprint?
01.int rowcount = sheet.getLastRowNum();
int rowcount = sheet.getLastRowNum();
7、取得一行的有效单元格个数view plaincopy to clipboardprint?
01.row.getLastCellNum();
row.getLastCellNum();
8、单元格值类型读写
view plaincopy to clipboardprint?
01.cell.setCellType(HSSFCell.CELL_TYPE_STRING); //设置单元格为STRING类型
02.cell.getNumericCellValue();//读取为数值类型的单元格内容
cell.setCellType(HSSFCell.CELL_TYPE_STRING); //设置单元格为STRING类型
cell.getNumericCellValue();//读取为数值类型的单元格内容
9、设置列宽、行高
view plaincopy to clipboardprint?
01.sheet.setColumnWidth((short)column,(short)width);
02.row.setHeight((short)height);
sheet.setColumnWidth((short)column,(short)width);
row.setHeight((short)height);
10、添加区域,合并单元格
view plaincopy to clipboardprint?
01.Region region = new Region((short)rowFrom,(short)columnFrom,(short)rowTo
02.,(short)columnTo);//合并从第rowFrom行columnFrom列
03.sheet.addMergedRegion(region);// 到rowTo行columnTo的区域
04.//得到所有区域
05.sheet.getNumMergedRegions()
Region region = new Region((short)rowFrom,(short)columnFrom,(short)rowTo
,(short)columnTo);//合并从第rowFrom行columnFrom列
sheet.addMergedRegion(region);// 到rowTo行columnTo的区域
//得到所有区域
sheet.getNumMergedRegions()
11、保存Excel文件
view plaincopy to clipboardprint?
01.FileOutputStream fileOut = new FileOutputStream(path);
02.wb.write(fileOut);
FileOutputStream fileOut = new FileOutputStream(path);
wb.write(fileOut);
12、根据单元格不同属性返回字符串数值
view plaincopy to clipboardprint?
01.public String getCellStringValue(HSSFCell cell) {
02. String cellValue = "";
03. switch (cell.getCellType()) {
04. case HSSFCell.CELL_TYPE_STRING://字符串类型
05. cellValue = cell.getStringCellValue();
06. if(cellValue.trim().equals("")||cellValue.trim().length()<=0)
07. cellValue=" ";
08. break;
09. case HSSFCell.CELL_TYPE_NUMERIC: //数值类型
10. cellValue = String.valueOf(cell.getNumericCellValue());
11. break;
12. case HSSFCell.CELL_TYPE_FORMULA: //公式
13. cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
14. cellValue = String.valueOf(cell.getNumericCellValue());
15. break;
16. case HSSFCell.CELL_TYPE_BLANK:
17. cellValue=" ";
18. break;
19. case HSSFCell.CELL_TYPE_BOOLEAN:
20. break;
21. case HSSFCell.CELL_TYPE_ERROR:
22. break;
23. default:
24. break;
25. }
26. return cellValue;
27. }
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;
}
13、常用单元格边框格式
view plaincopy to clipboardprint?
01.HSSFCellStyle style = wb.createCellStyle();
02.style.setBorderBottom(HSSFCellStyle.BORDER_DOTTED);//下边框
03.style.setBorderLeft(HSSFCellStyle.BORDER_DOTTED);//左边框
04.style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
05.style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_DOTTED);//下边框
style.setBorderLeft(HSSFCellStyle.BORDER_DOTTED);//左边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
14、设置字体和内容位置
view plaincopy to clipboardprint?
01.HSSFFont f = wb.createFont();
02.f.setFontHeightInPoints((short) 11);//字号
03.f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗
04.style.setFont(f);
05.style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
06.style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
07.style.setRotation(short rotation);//单元格内容的旋转的角度
08.HSSFDataFormat df = wb.createDataFormat();
09.style1.setDataFormat(df.getFormat("0.00%"));//设置单元格数据格式
10.cell.setCellFormula(string);//给单元格设公式
11.style.setRotation(short rotation);//单元格内容的旋转的角度
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);//单元格内容的旋转的角度
15、插入图片
view plaincopy to clipboardprint?
01.//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
02. ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
03. BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));
04. ImageIO.write(bufferImg,"jpg",byteArrayOut);
05.//读进一个excel模版
06.FileInputStream fos = new FileInputStream(filePathName+"/stencil.xlt");
07.fs = new POIFSFileSystem(fos);
08.//创建一个工作薄
09.HSSFWorkbook wb = new HSSFWorkbook(fs);
10.HSSFSheet sheet = wb.getSheetAt(0);
11.HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
12.HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1023,255,(short) 0,0,(short)10,10);
13.patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
//先把读进来的图片放到一个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));
16、调整工作表位置
view plaincopy to clipboardprint?
01.HSSFWorkbook wb = new HSSFWorkbook();
02.HSSFSheet sheet = wb.createSheet("format sheet");
03.HSSFPrintSetup ps = sheet.getPrintSetup();
04.sheet.setAutobreaks(true);
05.ps.setFitHeight((short)1);
06.ps.setFitWidth((short)1);
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);
17、设置打印区域
view plaincopy to clipboardprint?
01.HSSFSheet sheet = wb.createSheet("Sheet1");
02.wb.setPrintArea(0, "$A$1:$C$2");
HSSFSheet sheet = wb.createSheet("Sheet1");
wb.setPrintArea(0, "$A$1:$C$2");
18、标注脚注
view plaincopy to clipboardprint?
01.HSSFSheet sheet = wb.createSheet("format sheet");
02.HSSFFooter footer = sheet.getFooter()
03.footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
HSSFSheet sheet = wb.createSheet("format sheet");
HSSFFooter footer = sheet.getFooter()
footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
19、在工作单中清空行数据,调整行位置
view plaincopy to clipboardprint?
01.HSSFWorkbook wb = new HSSFWorkbook();
02.HSSFSheet sheet = wb.createSheet("row sheet");
03.// Create various cells and rows for spreadsheet.
04.// Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
05.sheet.shiftRows(5, 10, -5);
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);
20、选中指定的工作表
view plaincopy to clipboardprint?
01.HSSFSheet sheet = wb.createSheet("row sheet");
02.heet.setSelected(true);
HSSFSheet sheet = wb.createSheet("row sheet");
heet.setSelected(true);
21、工作表的放大缩小
view plaincopy to clipboardprint?
01.HSSFSheet sheet1 = wb.createSheet("new sheet");
02.sheet1.setZoom(1,2); // 50 percent magnification
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.setZoom(1,2); // 50 percent magnification
22、头注和脚注
view plaincopy to clipboardprint?
01.HSSFSheet sheet = wb.createSheet("new sheet");
02.HSSFHeader header = sheet.getHeader();
03.header.setCenter("Center Header");
04.header.setLeft("Left Header");
05.header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
06.HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16");
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");
23、自定义颜色
view plaincopy to clipboardprint?
01.HSSFCellStyle style = wb.createCellStyle();
02.style.setFillForegroundColor(HSSFColor.LIME.index);
03.style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
04.HSSFFont font = wb.createFont();
05.font.setColor(HSSFColor.RED.index);
06.style.setFont(font);
07.cell.setCellStyle(style);
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);
24、填充和颜色设置
view plaincopy to clipboardprint?
01.HSSFCellStyle style = wb.createCellStyle();
02.style.setFillBackgroundColor(HSSFColor.AQUA.index);
03.style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
04.HSSFCell cell = row.createCell((short) 1);
05.cell.setCellValue("X");
06.style = wb.createCellStyle();
07.style.setFillForegroundColor(HSSFColor.ORANGE.index);
08.style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
09.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);
25、强行刷新单元格公式
view plaincopy to clipboardprint?
01.HSSFFormulaEvaluator eval=new HSSFFormulaEvaluator((HSSFWorkbook) wb);
02.private static void updateFormula(Workbook wb,Sheet s,int row){
03. Row r=s.getRow(row);
04. Cell c=null;
05. FormulaEcaluator eval=null;
06. if(wb instanceof HSSFWorkbook)
07. eval=new HSSFFormulaEvaluator((HSSFWorkbook) wb);
08. else if(wb instanceof XSSFWorkbook)
09. eval=new XSSFFormulaEvaluator((XSSFWorkbook) wb);
10. for(int i=r.getFirstCellNum();i<r.getLastCellNum();i++){
11. c=r.getCell(i);
12. if(c.getCellType()==Cell.CELL_TYPE_FORMULA)
13. eval.evaluateFormulaCell(c);
14. }
15. }
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<r.getLastCellNum();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提供了静态方法evaluateAllFormu
laCells(HSSFWorkbook wb) ,计算一个Excel文件的所有公式,用起来很方便。