Apache POI是一个开源的Java读写Excel,WORD等微软OLE2组件文档的项目.目前POI已经有了Ruby版本
HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF - 提供读写Microsoft Word DOC97格式档案的功能。
XWPF - 提供读写Microsoft Word DOC2003格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读Microsoft Visio格式档案的功能。
HPBF - 提供读Microsoft Publisher格式档案的功能。
HSMF - 提供读Microsoft Outlook格式档案的功能。
public class PoiDemo {
public static void main(String[] args) throws Exception{
TextEasyExcel excel = new TextEasyExcel();
excel.Demo1();
}
public void Demo1() throws Exception{
Workbook wb = new HSSFWorkbook(); //创建一个工作簿
FileOutputStream out = new FileOutputStream("E:/你好啊.xls"); //定义一个输出流
wb.write(out);
out.close();
}
public void Demo2() throws Exception{
Workbook wb = new HSSFWorkbook(); //创建一个工作簿
wb.createSheet("sheet01"); //创建工作表sheet01
wb.createSheet("sheet02"); //创建工作表sheet02
FileOutputStream out = new FileOutputStream("E:/你好啊.xls");
wb.write(out);
out.close();
}
public void Demo3() throws Exception{
Workbook wb = new HSSFWorkbook(); //创建一个工作簿
Sheet sheet = wb.createSheet("sheet01"); //创建工作表sheet01
Row row = sheet.createRow(0); //创建行 (第一行)
Cell cell = row.createCell(0); //创建单元格 (第一行第一列)
cell.setCellValue("成功"); //给单元格设置值
FileOutputStream out = new FileOutputStream("E:/你好啊.xls");
wb.write(out);
out.close();
}
public void Demo4() throws Exception{
Workbook wb = new HSSFWorkbook(); //创建一个工作簿
Sheet sheet = wb.createSheet("sheet01"); //创建工作表sheet01
Row row = sheet.createRow(0); //创建行 (第一行)
Cell cell = row.createCell(0); //创建单元格 (第一行第一列)
cell.setCellValue(new Date()); //给单元格设置值 (时间)
//设置时间格式的单元格1
CreationHelper creationHelper = wb.getCreationHelper();
CellStyle cellStyle = wb.createCellStyle(); //单元格样式
cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-mm-dd hh:mm:ss"));
//1
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
//2
cell=row.createCell(2);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);
FileOutputStream out = new FileOutputStream("E:/你好啊.xls");
wb.write(out);
out.close();
}
public void Demo5() throws Exception{
Workbook wb = new HSSFWorkbook(); //创建一个工作簿
Sheet sheet = wb.createSheet("sheet01"); //创建工作表sheet01
Row row = sheet.createRow(0); //创建行 (第一行)
Cell cell = row.createCell(0); //创建单元格 (第一行第一列)
cell.setCellValue(new Date()); //给单元格设置值
FileOutputStream out = new FileOutputStream("E:/你好啊.xls");
wb.write(out);
out.close();
}
public void Demo6() throws Exception{
FileInputStream in = new FileInputStream("E:/你好啊.xls");
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0); //获取第一个sheet页
if(sheet == null){
return;
}
List<List<String>> lists = new ArrayList<>();
List<String> list = null;
//遍历行Row
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
HSSFRow row = sheet.getRow(i);
if(row == null){
continue;
}
list = new ArrayList<>();
list.add(row.getCell(0).toString());
list.add(row.getCell(1).toString());
lists.add(list);
for (int j = 0; j <= row.getLastCellNum(); j++) {
HSSFCell cell = row.getCell(j);
if(cell == null){
continue;
}
//System.out.print(" " + cell);
}
}
for(List ls : lists){
System.out.println(ls.get(0));
System.out.println(ls.get(1));
}
}
public void Demo7() throws Exception{
FileInputStream in = new FileInputStream("E:/你好啊.xls");
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs);
ExcelExtractor excelExtractor = new ExcelExtractor(wb);
//excelExtractor.setIncludeSheetNames(false); //不需要sheet名字
System.out.println(excelExtractor.getText());
}
}