创建2003excel表格
@Test
public void writeExcel2003() throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("hello world");
HSSFRow row = sheet.createRow(2);
HSSFCell cell = row.createCell(2);
cell.setCellValue("hello world!");
FileOutputStream fos = new FileOutputStream("d:\\yun\\hello2003.xls");
workbook.write(fos);
fos.close();
}
读取2003excel表格
@Test
public void readExcel2003() throws Exception {
FileInputStream fis = new FileInputStream("d:\\yun\\hello2003.xls");
HSSFWorkbook workbook = new HSSFWorkbook(fis);
HSSFSheet sheet = workbook.getSheet("hello world");
HSSFRow row = sheet.getRow(2);
HSSFCell cell = row.getCell(2);
String cellValue = cell.getStringCellValue();
System.out.println("单元格的值是:"+cellValue);
fis.close();
}
创建2007表格
@Test
public void writeExcel2007() throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("hello world");
XSSFRow row = sheet.createRow(2);
XSSFCell cell = row.createCell(2);
cell.setCellValue("hello world!");
FileOutputStream fos = new FileOutputStream("d:\\yun\\hello2007.xlsx");
workbook.write(fos);
fos.close();
}
读取2003excel表格
@Test
public void readExcel2003And2007() throws Exception {
String fileName = "d:\\yun\\hello2007.xlsx";
FileInputStream fis = new FileInputStream(fileName);
boolean b = fileName.matches("^.+(.xlsx)+$");
Workbook workbook = null;
workbook = (b) ? new XSSFWorkbook(fis) : new HSSFWorkbook(fis);
Sheet sheet = workbook.getSheet("hello world");
Row row = sheet.getRow(2);
Cell cell = row.getCell(2);
String cellValue = cell.getStringCellValue();
System.out.println("单元格的值是:"+cellValue);
fis.close();
workbook.close();
}
设置单元格样式
public CellStyle createStyle(Workbook workbook,int fontsize){
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
Font font = workbook.createFont();
font.setFontName(HSSFFont.FONT_ARIAL);
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) fontsize);
style.setFont(font);
return style;
}
设置带有样式的单元格
@Test
public void writeExcel2003Style() throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
CellRangeAddress cra = new CellRangeAddress(0,0,0,6);
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont font = workbook.createFont();
font.setFontName(HSSFFont.FONT_ARIAL);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setColor(HSSFColor.BLUE.index);
font.setFontHeightInPoints((short)16);
style.setFont(font);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(HSSFColor.GREEN.index);
HSSFSheet sheet = workbook.createSheet("hello world");
sheet.addMergedRegion(cra);
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("用户列表");
cell.setCellStyle(style);
FileOutputStream fos = new FileOutputStream("d:\\yun\\hello2003.xls");
workbook.write(fos);
fos.close();
}
生成list对象的excel的demo
@Test
public void writeExcel2003Users() throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
CellRangeAddress cra = new CellRangeAddress(0,0,0,6);
CellStyle style1 = createStyle(workbook,16);
CellStyle style2 = createStyle(workbook,12);
Sheet sheet = workbook.createSheet("hello world");
sheet.setDefaultColumnWidth(15);
sheet.addMergedRegion(cra);
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("用户列表");
cell.setCellStyle(style1);
String[] titles = {"用户名","帐号","所属部门","性别","手机号码","电子邮箱","生日"};
Row row2 = sheet.createRow(1);
for(int i = 0;i<titles.length;i++){
Cell cell1 = row2.createCell(i);
cell1.setCellValue(titles[i]);
cell1.setCellStyle(style2);
}
List<User> list = new ArrayList<User>();
list.add(new User(null,"测试","test",null,"部门A",null,true,"2344@qq.com","13888888888",null,new Date(),null));
list.add(new User(null,"测试2","test2",null,"部门B",null,true,"2344@163.com","13888889999",null,new Date(),null));
for(int i = 0;i<list.size();i++){
User user = list.get(i);
Row rowdata = sheet.createRow(i+2);
Cell cell0 = rowdata.createCell(0);
cell0.setCellValue(user.getName());
Cell cell1 = rowdata.createCell(1);
cell1.setCellValue(user.getAccount());
Cell cell2 = rowdata.createCell(2);
cell2.setCellValue(user.getDept());
Cell cell3 = rowdata.createCell(3);
cell3.setCellValue(user.isGender()?"男":"女");
Cell cell4 = rowdata.createCell(4);
cell4.setCellValue(user.getMobile());
Cell cell5 = rowdata.createCell(5);
cell5.setCellValue(user.getEmail());
Cell cell6 = rowdata.createCell(6);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
cell6.setCellValue(sdf.format(user.getBirthday()));
}
FileOutputStream fos = new FileOutputStream("d:\\yun\\hello2003.xls");
workbook.write(fos);
fos.close();
}