poi excel common operations

basic

Workbook wb= new HSSFWorkbook();
Sheet sheet = wb.createSheet("sheetName");
Row row = sheet.createRow(0); //第一行
Cell cell = row.createCell(0);//第一个单元格
cell.setCellValue("企业名称");//单元格赋值
cell.setCellStyle(cellStyle);//设置样式
FileOutputStream fos = new FileOutputStream("c:/workbook.xls");
wb.write(fos);
fos.close();

Merge Cells

CellRangeAddress (int firstRow, int lastRow, int firstCol, int lastCol)
parameters: start row number, end row number, start column number, end column number
Example:
CellRangeAddress cra =new CellRangeAddress(0, 1, 0, 0); //Merge the first column and two rows
sheet.addMergedRegion(cra);

Freeze and fix headers

Parameters of CreateFreezePane The
first parameter indicates the number of columns to be frozen;
the second parameter indicates the number of rows to be frozen, here only the column is frozen, so it is 0;
the third parameter indicates the sequence number of the first column visible in the right area, starting from 1 ;
The fourth parameter indicates the number of the first row visible in the lower area, which is also calculated from 1, here is the frozen column, so it is 0; for
example: sheet.createFreezePane(1,2,1,2);

Link

cell.setCellFormula("HYPERLINK(\"文件夹地址或者网址\",\"" + "附件"+ "\")");
//link样式
HSSFCellStyle linkStyle = (HSSFCellStyle) wb.createCellStyle();
HSSFFont cellFont= (HSSFFont) wb.createFont();
cellFont.setUnderline((byte) 1);
cellFont.setColor(HSSFColor.BLUE.index);
linkStyle.setFont(cellFont);
cell.setCellStyle(linkStyle);

format

文本:@
货币:#,##0.00
日期:yyyy年m月d日

例: HSSFCellStyle numberStyle = (HSSFCellStyle) wb.createCellStyle();
DataFormat format = wb.createDataFormat();
numberStyle.setDataFormat(format.getFormat("#,##0.00"));
cell.setCellStyle(numberStyle);
1,290,588.00

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325275576&siteId=291194637