java生成excel表格,多sheet,代码拿过来就能用呢~~~
pom文件:
<!--excel 2007版本-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.9</version>
</dependency>
java代码
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
/**
* Created by dell on 2018-04-12.
*/
public class ExportExcel {
//主程序测试
public static void main(String[] args) {
HashMap<String, Object> es_map = new HashMap<String, Object> ();
List<HashMap<String, Object>> ip_list = new LinkedList<HashMap<String, Object>>();
for (int i = 1; i <=10 ; i++) {
HashMap<String, Object> m1 = new HashMap<String, Object>();
m1.put("a",1*i);
m1.put("b",2*i);
m1.put("c",3*i);
ip_list.add(m1);
}
es_map.put("ip",ip_list);
List<HashMap<String, Object>> uv_list = new LinkedList<HashMap<String, Object>>();
for (int i = 1; i <=10 ; i++) {
HashMap<String, Object> m2 = new HashMap<String, Object>();
m2.put("d",1*i);
m2.put("e",2*i);
m2.put("f",3*i);
uv_list.add(m2);
}
es_map.put("uv",uv_list);
//生成excel表格并返回文件地址
String filepath = exportDetailData("20180401", "F://", es_map);
System.out.print(filepath);
}
/**
* @author yanawang
* @Title: exportDetailData
* @Description: 生成excel文件
* @date 2018-4-10 下午 05:45
*/
public static String exportDetailData(String dt, String basepath, HashMap<String, Object> es_map) {
SXSSFWorkbook workbook = new SXSSFWorkbook(100);
//第一个sheet
//表头
String classHead[] = {"A", "B", "C"};
//list中map的key值.与表头顺序需对应
String[] row_str = {"a", "b", "c"};
if (es_map.containsKey("ip")) {
setSheet(workbook, classHead, "日均独立IP", row_str, (List<HashMap<String, Object>>) es_map.get("ip"));
}
//第二个sheet
classHead = new String[]{"D", "E", "F"};
row_str = new String[]{"d", "e", "f"};
if (es_map.containsKey("uv")) {
setSheet(workbook, classHead, "新注册用户总数", row_str, (List<HashMap<String, Object>>) es_map.get("uv"));
}
//生成文件
File file1 = new File(basepath + "exportExcel");
file1.mkdir();
String fileName = "";
fileName = "weeklydata" + dt;
String filePath = "exportExcel/" + fileName + ".xlsx";
exportExcel(workbook, basepath + filePath);
return basepath + filePath;
}
/**
* @author yanawang
* @Title: setSheet
* @Description: 增加sheet
* @date 2018-4-11 下午 06:51
* 参数:表格对象,表头数据,sheet名称,list中map的key数组,list
*/
public static void setSheet(SXSSFWorkbook workbook, String[] classHead, String sheetname, String[] row_str, List<HashMap<String, Object>> list) {
if (list.size() > 0) {
//设置表格样式
CellStyle style = workbook.createCellStyle();
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
Sheet sheet = workbook.createSheet(sheetname);
CellStyle style1 = workbook.createCellStyle();
Row row_h = sheet.createRow(0);
//设置表头
for (int x = 0; x < classHead.length; x++) {
Cell cell = row_h.createCell(x);
cell.setCellStyle(style);
//单元格宽度
sheet.setColumnWidth(x, 3000);
cell.setCellValue(classHead[x]);
}
//设置单元格内容
for (int i = 0; i < list.size(); i++) {
Row row = sheet.createRow(i + 1);
for (int j = 0; j < row_str.length; j++) {
row.createCell(j).setCellValue(list.get(i).get(row_str[j]).toString());
}
}
}
}
/**
* @author yanawang
* @Title: exportExcel
* @Description: 生成excel文件流
* @date 2017-10-12 上午 09:18
*/
public static void exportExcel(Workbook workbook, String fileName) {
OutputStream os = null;
try {
os = new FileOutputStream(new File(fileName));
workbook.write(os);
System.out.println(fileName);
os.flush();
// os.close();
} catch (Exception e) {
} finally {
closeOs(os);
}
}
public static void closeOs(OutputStream out) {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
out = null;
}
}
}
网友评论