POI匯出excle,檔案匯出到本地
阿新 • • 發佈:2018-12-31
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;
public class ExportService {
@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;
}
}