关于POI的一个工具类

public class GradesXLS {

private String path;

public GradesXLS() {
super();
}

public GradesXLS(String path) {
super();
this.path = path;
}

/**
*
* @param dataList:数据
* @param column:列、列名
* @param clientName:文件名
* @param response
* @return
*/
public Boolean CreateExcel(List<Object[]> dataList, String[] column, String clientName, HttpServletResponse response) {
//创建一个工作簿
HSSFWorkbook workBook = new HSSFWorkbook();
//创建一个工作表,名为:第一页
HSSFSheet sheet = workBook.createSheet(clientName);
//设置单元格的宽度(0:表示第一行的第一个单元格,1:表示第一行的第二个单元格)
sheet.setColumnWidth((short) 0, 3500);
sheet.setColumnWidth((short) 1, 5000);
sheet.setColumnWidth((short) 2, 5000);
//创建一个单元格,从0开始
HSSFRow row = sheet.createRow((short) 0);
//构造一个数组设置第一行之后的单元格
HSSFCell[] cell = new HSSFCell[column.length];
for (int i = 0; i < column.length; i++) {
cell[i] = row.createCell(i);
cell[i].setCellValue(column[i]);
}

//获得从数据库中查询出来的数据
if (dataList != null && dataList.size() > 0) {
//循环list中的数据
for (int i = 0; i < dataList.size(); i++) {
Object[] obj = dataList.get(i);
HSSFRow dataRow = sheet.createRow(i);
HSSFCell data[] = new HSSFCell[column.length];
for (int j = 0; j < column.length; j++) {
data[j] = dataRow.createCell(i);
String info = String.valueOf(obj[j]);
data[j].setCellValue((info == null) ? "" : info);
}
}

try {
// String fileName = clientName + "-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";

//设置日期格式
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//new Date()获取当前系统时间,也可使用当前时间戳
String date = df.format(new Date());
String fileName = clientName + "-" + date + ".xls";
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("gbk"), "iso8859-1"));

OutputStream os = response.getOutputStream();
workBook.write();
os.flush();
return true;
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
}
return null;
}

}

posted @ 2018-02-05 17:08  桜に  阅读(29)  评论(0编辑  收藏  举报