写操作:
-
-
-
-
-
-
-
-
public class Test1 {
-
-
public static void main(String args[]) throws IOException {
-
-
-
-
-
-
Workbook workbook = new HSSFWorkbook();
-
-
Sheet sheet = workbook.createSheet("mysheet1");
-
for (int i=0;i<5;i++) {
-
-
Row row = sheet.createRow(i);
-
for (int j=0;j<5;j++) {
-
-
Cell cell = row.createCell(j);
-
-
cell.setCellValue("row"+(i+1)+",column"+(j+1));
-
}
-
}
-
-
OutputStream os = new FileOutputStream("file/test1.xls");
-
-
workbook.write(os);
-
os.close();
-
}
-
-
}
读操作:
-
public class Test4 {
-
-
public static void main(String args[]) throws IOException {
-
InputStream is = new FileInputStream("file/test1.xls");
-
Workbook wb = new HSSFWorkbook(is);
-
Sheet sheet = wb.getSheetAt(0);
-
-
for (Row row : sheet) {
-
for (Cell cell : row) {
-
int cellType = cell.getCellType();
-
switch (cellType) {
-
-
case Cell.CELL_TYPE_STRING:
-
String str = cell.getRichStringCellValue().getString();
-
System.out.println(str);
-
break;
-
case Cell.CELL_TYPE_BLANK:
-
if (DateUtil.isCellDateFormatted(cell)) {
-
System.out.println(cell.getDateCellValue());
-
} else {
-
System.out.println(cell.getNumericCellValue());
-
}
-
break;
-
case Cell.CELL_TYPE_FORMULA:
-
System.out.println(cell.getCellFormula());
-
break;
-
case Cell.CELL_TYPE_BOOLEAN:
-
System.out.println(cell.getBooleanCellValue());
-
break;
-
default:
-
System.out.println("---------------------");
-
break;
-
}
-
}
-
}
-
-
-
for (int i=0;i<sheet.getLastRowNum()+1;i++) {
-
Row row = sheet.getRow(i);
-
if (row==null)
-
continue;
-
for (int j=0;j<row.getLastCellNum();j++) {
-
Cell cell = row.getCell(j);
-
if (cell==null)
-
continue;
-
-
System.out.println(cell.getStringCellValue());
-
}
-
}
-
is.close();
-
}
-
-
}
合并单元格:
-
public static void main(String args[]) throws IOException {
-
Workbook wb = new HSSFWorkbook();
-
Sheet sheet = wb.createSheet("sheet1");
-
Row row = sheet.createRow(1);
-
Cell cell = row.createCell(1);
-
cell.setCellValue("a test of merge!");
-
-
sheet.addMergedRegion(new CellRangeAddress(
-
1,
-
1,
-
1,
-
3
-
));
-
OutputStream os = new FileOutputStream("file/test3.xls");
-
wb.write(os);
-
os.close();
-
}
换行:
-
public static void main(String args[]) throws IOException {
-
Workbook wb = new HSSFWorkbook();
-
Sheet sheet = wb.createSheet();
-
Row row = sheet.createRow(6);
-
sheet.autoSizeColumn(2);
-
for (int i=0;i<5;i++) {
-
Cell cell = row.createCell(i+2);
-
CellStyle style = wb.createCellStyle();
-
-
style.setWrapText(true);
-
-
cell.setCellValue("just use \n to wrap in a cell!");
-
cell.setCellStyle(style);
-
}
-
OutputStream os = new FileOutputStream("file/test6_newLine.xls");
-
wb.write(os);
-
os.close();
-
System.out.println("----------------------------");
-
}
画图:
-
-
-
-
-
-
-
-
-
-
-
-
HSSFPatriarch partriarch = (HSSFPatriarch) sheet5.createDrawingPatriarch();
-
HSSFSimpleShape shape = partriarch.createSimpleShape(new HSSFClientAnchor(0,0,0,0,(short)3,3,(short)5,5));
-
shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
-
-
shape.setFillColor(255,200,200);
-
shape.setLineStyle(HSSFSimpleShape.LINESTYLE_DASHGEL);
-
Poi对Excel的基本读写操作
原文:http://blog.csdn.net/elim168/article/details/40543049