package poi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
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;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class PoiDemo {
private static final Logger LOGGER = Logger.getLogger(PoiDemo.class);
public static void main(String[] args) throws CloudException {
List<Map<String, String>> writeBackList = new ArrayList<Map<String,String>>();
Map<String, String> writeBackMap1 = new LinkedHashMap<String, String>();
writeBackMap1.put("Service Number", "510002");
writeBackMap1.put("FUND", "AAA01");
writeBackMap1.put("AVERAGE_COST", "100008");
writeBackList.add(writeBackMap1);
Map<String, String> writeBackMap2 = new LinkedHashMap<String, String>();
writeBackMap2.put("Service Number", "510003");
writeBackMap2.put("FUND", "BBB01");
writeBackMap2.put("START_DATE", "06/25/2011");
writeBackMap2.put("END_DATE", "06/25/2011");
writeBackList.add(writeBackMap2);
String execlPath = "D:\\Outputs.xls";
String sheetName = "Output";
writeBackExcel(writeBackList, execlPath, sheetName);
}
private static void writeBackExcel(List<Map<String, String>> writeBackList,
String execlPath, String sheetName) throws CloudException {
try
{
// Get max size of Map
int maxLength = 0;
for (Map<String, String> map : writeBackList) {
if (map.size() > maxLength) {
maxLength = map.size();
}
}
// init
File file = new File(execlPath);
InputStream inputStream = new FileInputStream(file);
POIFSFileSystem execlFile = new POIFSFileSystem(inputStream);
HSSFWorkbook workbook = new HSSFWorkbook(execlFile);
HSSFSheet sheet = workbook.getSheet(sheetName);
if (sheet != null)
{
// delete
workbook.removeSheetAt(workbook.getSheetIndex(sheet));
}
sheet = workbook.createSheet(sheetName);
// write header lines
HSSFRow row = sheet.createRow(0);
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(HSSFColor.GREEN.index);
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle.setFont(font);
int index = 0;
for (int i = 0; i < maxLength * 2; i += 2) {
if (i == 0)
{
HSSFCell cell = row.createCell(0);
cell.setCellStyle(cellStyle);
cell.setCellValue("Service Number");
} else {
HSSFCell cell1 = row.createCell(i - 1);
cell1.setCellStyle(cellStyle);
cell1.setCellValue("Output" + index);
HSSFCell cell2 = row.createCell(i);
cell2.setCellStyle(cellStyle);
cell2.setCellValue("Output" + index + "_Val");
}
index++;
}
//write back records
for (int listIndex = 0; listIndex < writeBackList.size(); listIndex++) {
Map<String, String> map = writeBackList.get(listIndex);
HSSFRow recordRow = sheet.createRow(listIndex + 1);
int cellIndex = 0;
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (cellIndex == 0) {
HSSFCell cell = recordRow.createCell(0);
cell.setCellValue(value);
} else {
HSSFCell cell1 = recordRow.createCell(cellIndex - 1);
cell1.setCellValue(key);
HSSFCell cell2 = recordRow.createCell(cellIndex);
cell2.setCellValue(value);
}
cellIndex += 2;
}
}
OutputStream outputStream = new FileOutputStream(file);
workbook.write(outputStream);
outputStream.close();
inputStream.close();
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new CloudException(e.getMessage(), e);
}
}
}
POI write excel demo
5星 · 超过95%的资源 需积分: 9 138 浏览量
2011-06-25
13:32:31
上传
评论
收藏 5KB RAR 举报
WJ_kingom
- 粉丝: 4
- 资源: 24
最新资源
- 大学生方程式赛车设计(总体设计)
- 【哈工大计算机系统】CSAPP计统大作业
- 音视频对齐python脚本
- Tkinter简易版学生信息管理系统源码+数据库配置文件
- Advanced Sniper Starter kit v4.3.unitypackage
- tensorflow-gpu-2.8.3-cp37-cp37m-win-amd64.whl
- Android SMS 短信内容显示在一个 泡泡 形状的窗口中.7z
- 哈尔滨工业大学计算机组成原理2024春大作业 ICS PA
- tensorflow-2.8.4-cp37-cp37m-win-amd64.whl
- tensorflow-2.8.3-cp37-cp37m-win-amd64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈