package com.edu;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class TestPOI {
public static void Output() {
System.out.println("OK.");
}
public static void createWorkbook() throws Exception {
// create "xxx.xls"
Workbook wb = new HSSFWorkbook();
// create "xxx.xlsx"
// Workbook wb = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook_1.xls");
wb.write(fileOut);
fileOut.close();
Output();
}
public static void createSheet() throws Exception {
Workbook wb = new HSSFWorkbook();
// create a new sheet
Sheet sheet_1 = wb.createSheet("new sheet_1");
Sheet sheet_2 = wb.createSheet("new sheet_2");
FileOutputStream fileOut = new FileOutputStream("workbook_2.xls");
wb.write(fileOut);
fileOut.close();
Output();
}
public static void createCell() throws Exception {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Rows are 0 based
Row row = sheet.createRow(0);
// Cells are 0 based
Cell cell = row.createCell(0);
cell.setCellValue("宇智波鼬");
sheet.createRow(2).createCell(1).setCellValue("漩渦鳴人");
FileOutputStream fileOut = new FileOutputStream("workbook_3.xls");
wb.write(fileOut);
fileOut.close();
Output();
}
public static void createCellStyle() throws Exception {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
HSSFFont font = (HSSFFont) wb.createFont();
font.setColor(HSSFColor.VIOLET.index);
// font size
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
cell.setCellValue("火影忍者");
FileOutputStream fileout = new FileOutputStream("workbook_4.xls");
wb.write(fileout);
fileout.close();
Output();
}
public static void readWorkbook() throws Exception {
FileInputStream inp = new FileInputStream("xxx.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheet("xxx");
for (Row row : sheet) {
for (Cell cell : row) {
CellReference cellRef = new CellReference(row.getRowNum(),
cell.getColumnIndex());
System.out.print(cellRef.formatAsString());
System.out.print(" - ");
// To get the contents of a cell, you first need to know what
// kind of cell it is
// you will want to switch on the cell's type, and then call the
// appropriate getter for that cell.
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 static void main(String[] args) throws Exception {
// createWorkbook();
// createSheet();
// createCell();
// createCellStyle();
// readWorkbook();
}
}
评论29
最新资源