您的位置:首页 > 其它

poi操作excel文件

2017-07-28 16:18 337 查看

1.引入poi所需要的jar



2. 97-2003版以xls结尾的Excel文件读写

读取文件

/**
* 2003_xls 读取
* @param path
* @throws IOException
*/
@SuppressWarnings("deprecation")
public static void readExcel(String path) throws IOException{
File file = new File(path);
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream(file));
HSSFWorkbook hssfWorkbook =  new HSSFWorkbook(poifsFileSystem);
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);

int rowstart = hssfSheet.getFirstRowNum();
int rowEnd = hssfSheet.getLastRowNum();
for(int i=rowstart;i<=rowEnd;i++)
{
HSSFRow row = hssfSheet.getRow(i);
if(null == row) continue;
int cellStart = row.getFirstCellNum();
int cellEnd = row.getLastCellNum();

for(int k=cellStart;k<=cellEnd;k++)
{

HSSFCell cell = row.getCell((short) k);
if(null==cell) continue;
System.out.print(k + "  ");
//System.out.print("type:"+cell.getCellType());

switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC: // 数字
System.out.print(cell.getNumericCellValue()
+ "   ");
break;
case HSSFCell.CELL_TYPE_STRING: // 字符串
System.out.print(cell.getStringCellValue()
+ "   ");
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
System.out.println(cell.getBooleanCellValue()
+ "   ");
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
System.out.print(cell.getCellFormula() + "   ");
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println(" ");
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println(" ");
break;
default:
System.out.print("未知类型   ");
break;
}

}
System.out.print("\n");
}
}


写入文件

@SuppressWarnings("deprecation")
public static void writeExcel(List<Book> list){
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("图书表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
style.setFillForegroundColor(HSSFColor.YELLOW.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillBackgroundColor(HSSFColor.YELLOW.index);
//设置行高
row.setHeight((short) ((short) 256 * 1.5));
//设置列宽   256为一个字符单位
sheet.setColumnWidth((short) 0, (short) 256*25);
sheet.setColumnWidth((short) 5, (short) 256*80);

HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("书籍名称");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("书籍作者");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("评分");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("页数");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("豆瓣售价");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("豆瓣简介");
cell.setCellStyle(style);

// 第五步,写入实体数据 实际应用中这些数据从数据库得到,

for (int i = 0; i < list.size(); i++)
{
row = sheet.createRow((int) i + 1);
row.setHeight((short) ((short) 256* 1.5));
Book book = (Book) list.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue(book.getBook_name());
row.createCell((short) 1).setCellValue(book.getAuthor());
row.createCell((short) 2).setCellValue(book.getScore());
row.createCell((short) 3).setCellValue(book.getPage_count());
row.createCell((short) 4).setCellValue(book.getActual_price());
row.createCell((short) 5).setCellValue(book.getIntroduction());
}
// 第六步,将文件存到指定位置
try
{
FileOutputStream fout = new FileOutputStream("E:/豆瓣书籍.xls");
wb.write(fout);
fout.close();
System.out.println("写入Excel完毕!");
}
catch (Exception e)
{
e.printStackTrace();
}
}


3.2007版以xlsx结尾的excel

读取文件

/**
* 2007_xlsx 读取
* @param path
* @throws IOException
*/
public static void readExcel2(String path) throws IOException{
File file = new File(path);

XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file));
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);

int rowstart = xssfSheet.getFirstRowNum();
int rowEnd = xssfSheet.getLastRowNum();
for(int i=rowstart;i<=rowEnd;i++)
{
XSSFRow row = xssfSheet.getRow(i);
if(null == row) continue;
int cellStart = row.getFirstCellNum();
int cellEnd = row.getLastCellNum();

for(int k=cellStart;k<=cellEnd;k++)
{
XSSFCell cell = row.getCell(k);
if(null==cell) continue;

switch (cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC: // 数字
System.out.print(cell.getNumericCellValue()
+ "   ");
break;
case HSSFCell.CELL_TYPE_STRING: // 字符串
System.out.print(cell.getStringCellValue()
+ "   ");
break;
case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
System.out.println(cell.getBooleanCellValue()
+ "   ");
break;
case HSSFCell.CELL_TYPE_FORMULA: // 公式
System.out.print(cell.getCellFormula() + "   ");
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println(" ");
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println(" ");
break;
default:
System.out.print("未知类型   ");
break;
}

}
System.out.print("\n");
}
}


写入文件

public static void writeExcel2(List<Book> list){
// 第一步,创建一个webbook,对应一个Excel文件
XSSFWorkbook wb = new XSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
XSSFSheet sheet = wb.createSheet("图书表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
XSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
XSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

XSSFCell cell = row.createCell((sho
cf1b
rt) 0);
cell.setCellValue("书籍名称");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("书籍作者");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("评分");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("页数");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("豆瓣售价");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("豆瓣简介");
cell.setCellStyle(style);

// 第五步,写入实体数据 实际应用中这些数据从数据库得到,

for (int i = 0; i < list.size(); i++)
{
row = sheet.createRow((int) i + 1);
row.setHeight((short) ((short) 256* 1.5));
Book book = (Book) list.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue(book.getBook_name());
row.createCell((short) 1).setCellValue(book.getAuthor());
row.createCell((short) 2).setCellValue(book.getScore());
row.createCell((short) 3).setCellValue(book.getPage_count());
row.createCell((short) 4).setCellValue(book.getActual_price());
row.createCell((short) 5).setCellValue(book.getIntroduction());
}
// 第六步,将文件存到指定位置
try
{
FileOutputStream fout = new FileOutputStream("E:/豆瓣图书07.xlsx");
wb.write(fout);
fout.close();
System.out.println("写入Excel完毕!");
}
catch (Exception e)
{
e.printStackTrace();
}
}


测试

public static void main(String[] args) {
testWriteExcel();

//     try {
//          //readExcel2("E:/遗漏数据汇总.xlsx");
//          readExcel("E:/测试.xls");
//     } catch (IOException e) {
//          e.printStackTrace();
//     }

}

public static void testWriteExcel(){
Connection conn = C3P0Util.getConnection();
String sql  ="select * from book_info";
List<Book> list= DBUtil.query(sql, conn);
writeExcel2(list);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: