驼背鲸javaexcel处理框架,Java三⽅—-excel框架之POI的使⽤⼀Apache POI是Apache软件基⾦会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。pdf框架之IText的使⽤,参见我的博客:Java三⽅—->pdf框架之IText的使⽤。今天我们开始POI中Excel部分的学习。
POI框架的简单实例
我们通过案例来学习POI,项⽬结构如下:
⼀、 创建⼯作薄
@Testdentist
public void createExcel() throws Exception {
// 建⽴xls⽂件,只需要引⼊poi-3.14-20160307.jar
Workbook wb1 = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("excel/workbook.xls");
wb1.write(fileOut);
pp2fileOut.clo();
wb1.clo();
// 需要额外引⼊poi-ooxml-3.14-20160307.jar, xmlbeans-2.6.0.jar, poi-ooxml-schemas-3.14-20160307.jarwap是什么意思
Workbook wb2 = new XSSFWorkbook();
FileOutputStream fileOut2 = new FileOutputStream("excel/workbook.xlsx");
wb2.write(fileOut2);
fileOut2.clo();
wb2.clo();
}
⼆、 创建⼯作表
@Test
public void createExce2() throws Exception {
Workbook wb = new HSSFWorkbook();
String safeName = ateSafeSheetName("[O'Brien's sales*?]s"); // returns " O'Brien's sales "
善意的谎言
FileOutputStream fileOut = new FileOutputStream("excel/workbook1.xls");
wb.write(fileOut);
fileOut.clo();
wb.clo();
}
注意: sheet name is Excel must not exceed 31 characters and must not contain any of the any of the following characters:
0x0000
0x0003路透社英文
colon (:)
backslash (\)
asterisk (*)
question mark (?)
forward slash (/)
opening square bracket ([)
closing square bracket (])
You can u org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)} for a safe way to create valid names, this utility replaces invalid characters with a space (‘ ‘)
三、 创建单元格
@Test
public void createExce3() throws Exception {
Workbook wb = new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 bad.
Row row = ateRow((short) 0);
// Create a cell and put a value in it.
Cell cell = ateCell(0);
cell.tCellValue(1);
// Or do it on one line.
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("excel/workbook2.xls");
wb.write(fileOut);
fileOut.clo();
wb.clo();
}
永远在一起英文四、 创建时间单元格
@Test
public void createExce4() throws Exception {
Workbook wb = new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
/
/ Create a row and put some cells in it. Rows are 0 bad.
Row row = ateRow(0);
beg的过去式// Create a cell and put a date value in it. The first cell is not
// styled
// as a date.
Cell cell = ateCell(0);
cell.tCellValue(new Date());
// we style the cond cell as a date (and time). It is important to
// create a new cell style from the workbook otherwi you can end up
// modifying the built in style and effecting not only this cell but
// other cells.
CellStyle cellStyle = wb.createCellStyle();
cellStyle.ateDataFormat().getFormat("m/d/yy h:mm")); cell = ateCell(1);
cell.tCellValue(new Date());
cell.tCellStyle(cellStyle);
// you can also t date as java.util.Calendar
cell = ateCell(2);
cell.Instance());
cell.tCellStyle(cellStyle);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("excel/workbook3.xls"); wb.write(fileOut);
fileOut.clo();
wb.clo();
}
五、 不同类型的单元格
@Test
public void createExce5() throws Exception {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = ateRow((short) 2);
draw怎么读的ateCell(0).tCellValue(1.1);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("excel/workbook4.xls"); wb.write(fileOut);
fileOut.clo();
wb.clo();
}
六、 边框的使⽤
@Test
public void createExce6() throws Exception {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = ateRow(1);
// Create a cell and put a value in it.
Cell cell = ateCell(1);
cell.tCellValue(4);
// Style the cell with borders all around.
CellStyle style = wb.createCellStyle();
style.tBorderBottom(CellStyle.BORDER_THIN);
style.tBottomBorderColor(Index());
style.tBorderLeft(CellStyle.BORDER_THIN);
style.tLeftBorderColor(Index());
style.tBorderRight(CellStyle.BORDER_THIN);
style.tRightBorderColor(Index());
style.tBorderTop(CellStyle.BORDER_MEDIUM_DASHED);
style.tTopBorderColor(Index());
cell.tCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("excel/workbook5.xls"); wb.write(fileOut);
fileOut.clo();
wb.clo();
}
七、合并单元格
@Test
public void createExce7() throws Exception {
routerWorkbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = ateRow((short) 1);
Cell cell = ateCell((short) 1);
cell.tCellValue("This is a test of merging");
sheet.addMergedRegion(new CellRangeAddress(1, // first row (0-bad) 1, // last row (0-bad)
1, // first column (0-bad)
2 // last column (0-bad)
));
// Write the output to a file