poi的入门学习
1.基本使用
package cn.itcast.poi.test;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class PoiTest06 {
public static void main(String[] args) {
Workbook wb = null;
FileOutputStream os =null;
try {
wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("abc");
os = new FileOutputStream("E:\\excel\\poi\\abc.xlsx");
wb.write(os);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(os!=null) os.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (wb!=null)wb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.定位需要填写数据的单元格(导出Excel文件)
package cn.itcast.poi.test;
import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class PoiTest07 {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet1");
Row row = sheet.createRow(2);
Cell cell = row.createCell(2);
cell.setCellValue("我的第一个poi学习笔记");
FileOutputStream os = new FileOutputStream("E:\\excel\\poi\\test2.xlsx");
workbook.write(os);
workbook.close();
os.close();
}
}
3.创建单元格的样式
package cn.itcast.poi.test;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class PoiTest08 {
public static void main(String[] args) throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow(2);
Cell cell = row.createCell(2);
cell.setCellValue("我的第一个poi笔记");
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
Font font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 15);
cellStyle.setFont(font);
row.setHeightInPoints(15.0f);
sheet.setColumnWidth(2,31*256);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cell.setCellStyle(cellStyle);
FileOutputStream os = new FileOutputStream("E:\\excel\\poi\\test3.xlsx");
wb.write(os);
wb.close();
os.close();
}
}
4.读取(导入)Excel文件数据
package cn.itcast.poi.test;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class PoiTest09 {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook("E:\\excel\\poi\\demo.xlsx");
Sheet sheet = workbook.getSheetAt(0);
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
StringBuilder builder = new StringBuilder();
for (int j = 2; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
Object value = getValue(cell);
builder.append(value).append("-");
}
System.out.println(builder.toString());
}
}
public static Object getValue(Cell cell) {
CellType type = cell.getCellType();
Object object = null;
switch (type) {
case STRING:
object = cell.getStringCellValue();
break;
case BOOLEAN:
object = cell.getBooleanCellValue();
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
object = cell.getDateCellValue();
} else {
object = cell.getNumericCellValue();
}
break;
case FORMULA:
object = cell.getCellFormula();
break;
default:
break;
}
return object;
}
}