/**
* 导出Excel
* @param sheetName sheet名称
* @param title 标题
* @param values 内容
* @param wb HSSFWorkbook对象
* @return
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if(wb == null){
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
sheet.setDefaultColumnWidth(10);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCellStyle headerStyle = wb.createCellStyle();
headerStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 14);
font.setFontName("黑体");
// 把字体应用到当前的样式
headerStyle.setFont(font);
//声明列对象
HSSFCell cell = null;
//创建标题
for(int i=0;i<title.length;i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(headerStyle);
}
//创建内容
for(int i=0;i<values.length;i++){
row = sheet.createRow(i + 1);
for(int j=0;j<values[i].length;j++){
//将内容按顺序赋给对应的列对象
cell = row.createCell(j);
cell.setCellValue(values[i][j]);
cell.setCellStyle(style);
}
}
//文字自适应
for(int i=0;i<title.length;i++) {
sheet.autoSizeColumn(i+1);
}
return wb;
}
-
//发送响应流方法
public static void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(),"ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 根据fileType不同读取excel文件
*
* @param path
* @param path
* @throws IOException
*/
public static List<List<String>> readExcel(String path) {
String fileType = path.substring(path.lastIndexOf(".") + 1);
// return a list contains many list
List<List<String>> lists = new ArrayList<List<String>>();
//读取excel文件
InputStream is = null;
try {
is = new FileInputStream(path);
//获取工作薄
Workbook wb = null;
if (fileType.equals("xls")) {
wb = new HSSFWorkbook(is);
} else if (fileType.equals("xlsx")) {
wb = new XSSFWorkbook(is);
} else {
return null;
}
//读取第一个工作页sheet
Sheet sheet = wb.getSheetAt(0);
//第一行为标题
for (Row row : sheet) {
ArrayList<String> list = new ArrayList<String>();
for (Cell cell : row) {
//根据不同类型转化成字符串
cell.setCellType(Cell.CELL_TYPE_STRING);
list.add(cell.getStringCellValue());
}
lists.add(list);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return lists;
}
/**
* 设置excel表头样式
* @param headerStyle
* @param wb
* @return
*/
public static HSSFCellStyle setHeadCss(HSSFCellStyle headerStyle,HSSFWorkbook wb) {
headerStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 13);
font.setFontName("黑体");
// 把字体应用到当前的样式
headerStyle.setFont(font);
return headerStyle;
}