环境:jdk1.8,eclipse
导出成excel:
jar包如下:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelUtil {
static String path = "C:\\Users\\A\\Desktop\\everyDay\\write\\test0514\\aa.xlsx";
public static void main(String[] args) {
new ExcelUtil().dowork();
}
/**
* Date:2019年5月14日下午5:50:03
*/
private void dowork() {
List<Map<String, String>> end = new ArrayList<Map<String, String>>();
Map<String, String> m = new HashMap<String, String>();
m.put("name", "张三");
m.put("age", "3");
end.add(m);
Map<String, String> ma = new HashMap<String, String>();
ma.put("name", "李四");
ma.put("age", "21");
end.add(ma);
outFile(expExcel(end));
}
public static void outFile(HSSFWorkbook workbook) {
OutputStream os = null;
try {
os = new FileOutputStream(new File(path));
workbook.write(os);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建excel并填入数据
*/
public static HSSFWorkbook expExcel(List<Map<String, String>> body) {
// 创建一个excel工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建一个sheet工作表
HSSFSheet sheet = workbook.createSheet("学生信息");
// 创建第0行表头,再在这行里在创建单元格,并赋值
HSSFRow row = sheet.createRow(0);
HSSFCell cell = null;
// ====style start
HSSFCellStyle cellStyle = workbook.createCellStyle();
setBorderStyle(cellStyle);
setFontStyle(workbook, "黑体", (short) 14);
// ====style end
String headStr = "姓名,身份证";
String[] headSplit = headStr.split(",");
for (int i = 0; i < headSplit.length; i++) {
cell = row.createCell(i);
cell.setCellValue(headSplit[i]);// 设置值
// set cellStyle
cell.setCellStyle(cellStyle);
}
// 将主体数据填入Excel中
for (int i = 0; i < body.size(); i++) {
row = sheet.createRow(i + 1);
Map<String, String> map = body.get(i);
Set<String> keySet = map.keySet();
int cc = 0;
for (String s : keySet) {
cell = row.createCell(cc);
cc++;
cell.setCellValue(map.get(s));// 设置值
// set cellStyle
cell.setCellStyle(cellStyle);
}
}
return workbook;
}
/**
* 设置字体样式
*
* @param workbook
* 工作簿
* @param name
* 字体类型
* @param height
* 字体大小
* @return HSSFFont
*/
private static HSSFFont setFontStyle(HSSFWorkbook workbook, String name, short height) {
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints(height);
font.setFontName(name);
return font;
}
/**
* 设置单元格样式
*
* @param workbook
* 工作簿
*/
private static void setBorderStyle(HSSFCellStyle cellStyle) {
cellStyle.setBorderBottom((short) 1); // 下边框
cellStyle.setBorderLeft((short) 1);// 左边框
cellStyle.setBorderTop((short) 1);// 上边框
cellStyle.setBorderRight((short) 1);// 右边框
}
}
结果图: