java导出excel,自定义列与行
2014-04-28 09:40
239 查看
private void createNewFolder(String folderPath) {
try {
File myFilePath = new File(folderPath);
if (!myFilePath.exists()) {
myFilePath.mkdir();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public String downloadMore(List<String[]> tjList, String name) {
try {
//name=new String(name.getBytes("ISO-8859-1"),"gbk");
System.out.println("--name"+name);
String subname=name;
String[] strs=name.split(";");
if(strs!=null&&strs.length>2)
{
subname=strs[0];//0:名字,1:第一个列民,2:第二个列民
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String time=sdf.format(date);
int a=(int)(Math.random()*100000);
// 通过参数拼接出文件名称
String fileName =time +"-"+a+ ".xls";
// 查看本地是否存在 存在直接下载
String enc = "UTF-8";
String realPath = ServletActionContext.getServletContext().getRealPath("");
createNewFolder(realPath + "\\download\\");
File downloadFile = new File(realPath + "\\download\\" + fileName);
if (!downloadFile.exists()) {
FileOutputStream output = new FileOutputStream(downloadFile);
// 通过service获取到数据集合 ,然后生产 xls保存到本地
// 将结果集生成 xls
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(subname + "_" + time);
//HSSFSheet hws=workbook.getSheet(name);
HSSFRow rowTitle = sheet.createRow(0);
HSSFCell titleCell = rowTitle.createCell(0);
titleCell.setCellValue("编号");
for(int i=1;i<strs.length;i++)
{
titleCell = rowTitle.createCell(i);
titleCell.setCellValue(strs[i]);
}
for (int rowIndex = 0; rowIndex < tjList.size(); rowIndex++) {
String[] tj=tjList.get(rowIndex);
HSSFRow row = sheet.createRow(rowIndex + 1);
HSSFCell cell = row.createCell(0);
cell.setCellValue(new HSSFRichTextString(rowIndex+""));
for(int k=0;k<tj.length;k++)
{
cell = row.createCell(k+1);
cell.setCellValue(new HSSFRichTextString(tj[k]));
}
}
sheet.setColumnWidth(0, 0);
workbook.write(output);
}
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/octet-stream");
long length = downloadFile.length();
response.setContentLength(Long.valueOf(length).intValue());
fileName = URLEncoder.encode(downloadFile.getName(), enc);
response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
ServletOutputStream servletOutputStream = response.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(downloadFile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
int size = 0;
byte[] b = new byte[4096];
while ((size = bufferedInputStream.read(b)) != -1) {
servletOutputStream.write(b, 0, size);
}
servletOutputStream.flush();
servletOutputStream.close();
bufferedInputStream.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
参数说明:
List<String[]>tjList ,String name :name是由;隔开的一串字符串,第一个为标题,第二个为行名(所有行名一样的情况),从第三个开始为列名,可以自定义个数,但要和tjList中的个数对应
此方法其实还可以继续改造的,标题,行名可以传单独的参数过来,列名可以用一个数组传过来,这样更方便明了,容易看懂
相关文章推荐
- Java导出Excel设置大数据不被科学计数法显示、自定义日期格式
- Java自定义导出Excel
- java自定义注解在excel导出时的使用
- java 导出Excel 自定义Excel模版及表达式(毛坯)
- Excel数据导出,Java自定义注解
- Java 实现Excel导入导出(包含一些简单样式设置)工具类
- Net 自定义Excel模板导出数据
- 关于Java导出100万行数据到Excel的优化方案
- java导出excel设置表头
- Java 导出数据到excel
- java基础之导出(Excel)
- Java导入、导出Excel
- JAVA使用POI根据模版导出EXCEL
- 【叨、校长】JAVA WEB 导出Excel报表
- java导出大量的excel
- java大数据量的excel导入导出
- java通过jxl.jar实现excel导入导出
- Java大批量导出数据,格式可以为xml或excel
- Java实现Excel文件导入导出(三)
- javaweb 利用poi 导出excel