import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import javax.annotation.Resource;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
@Override
public String CreateExcelDemo(ArrayList list) throws Exception {
String fileName="";
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("学生表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
//style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("序号");
cell = row.createCell((short) 1);
cell.setCellValue("姓名");
cell = row.createCell((short) 2);
cell.setCellValue("住址");
cell = row.createCell((short) 3);
for(int i=0;i<list.size();i++){
HashMap map =(HashMap)list.get(i);
// 第四步,创建单元格,并设置值
row = sheet.createRow((int) i+1);
HSSFCell celli = row.createCell((short) 0);
row.createCell((short) 0).setCellValue(i);
if(map.get("JGOBJECTNAME")!=null)
row.createCell((short) 1).setCellValue((String) map.get("JGOBJECTNAME"));
if(map.get("ZHUSUO")!=null)
row.createCell((short) 2).setCellValue((String) map.get("ZHUSUO"));
if(map.get("SHXYDM")!=null)
row.createCell((short) 3).setCellValue((String) map.get("SHXYDM"));
if(map.get("LXR")!=null)
row.createCell((short) 4).setCellValue((String) map.get("LXR"));
}
// 第六步,将文件存到指定位置
try {
fileName = "E:/"+UUID.randomUUID().toString()+".xls";
FileOutputStream fout = new FileOutputStream(fileName);
wb.write(fout);
fout.close();
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
return fileName;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import javax.annotation.Resource;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Override
public String CreateExcelDemo(ArrayList list) throws Exception {
String fileName="";
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("学生表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
//style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("序号");
cell = row.createCell((short) 1);
cell.setCellValue("姓名");
cell = row.createCell((short) 2);
cell.setCellValue("住址");
cell = row.createCell((short) 3);
cell.setCellValue("班级");
cell = row.createCell((short) 4);
cell.setCellValue("年级");
for(int i=0;i<list.size();i++){
HashMap map =(HashMap)list.get(i);
// 第四步,创建单元格,并设置值
row = sheet.createRow((int) i+1);
HSSFCell celli = row.createCell((short) 0);
row.createCell((short) 0).setCellValue(i);
if(map.get("JGOBJECTNAME")!=null)
row.createCell((short) 1).setCellValue((String) map.get("JGOBJECTNAME"));
if(map.get("ZHUSUO")!=null)
row.createCell((short) 2).setCellValue((String) map.get("ZHUSUO"));
if(map.get("SHXYDM")!=null)
row.createCell((short) 3).setCellValue((String) map.get("SHXYDM"));
if(map.get("LXR")!=null)
row.createCell((short) 4).setCellValue((String) map.get("LXR"));
}
// 第六步,将文件存到指定位置
try {
fileName = "E:/"+UUID.randomUUID().toString()+".xls";
FileOutputStream fout = new FileOutputStream(fileName);
wb.write(fout);
fout.close();
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
return fileName;
}
}