从list中取数据把特殊的数据进行封装来进行导出
package com.sjy.poi;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TestExportExcelSheets {
public static void main(String[] args) throws IOException {
XSSFWorkbook xwb = new XSSFWorkbook();
Map<String,List<Map<String,String>>> map = new TreeMap<String,List<Map<String,String>>>();
List<Map<String,String>> list1 = new ArrayList<Map<String,String>>();
List<Map<String,String>> list2 = new ArrayList<Map<String,String>>();
Map<String,String> map3 = new TreeMap<String,String>();
map3.put("国家", "china");
map3.put("数据采样时间", "2014/10/12");
map3.put("运营商", "sprint");
Map<String,String> map4 = new TreeMap<String,String>();
map4.put("国家", "america");
map4.put("数据采样时间", "2014/10/12");
map4.put("运营商", "sprint");
Map<String,String> map5 = new TreeMap<String,String>();
map5.put("NAME", "america");
map5.put("NAME_CN", "aaa");
map5.put("NAME_EN", "111");
map5.put("NAME_TYPE", "2014/10/12");
map5.put("SCOPE", "sprint");
map5.put("INDEX_TABLE", "INDEX_VALUE");
map5.put("INDEX_LOGIC", "america");
map5.put("DESC_CN", "america");
map5.put("DESC_EN", "america");
map5.put("EXT1", "america");
map5.put("EXT2", "america");
map5.put("EXT3", "america");
Map<String,String> map6 = new TreeMap<String,String>();
map6.put("NAME", "america");
map6.put("NAME_CN", "bbb");
map6.put("NAME_EN", "222");
map6.put("NAME_TYPE", "2014/10/12");
map6.put("SCOPE", "sprint");
map6.put("INDEX_TABLE", "INDEX_VALU");
map6.put("INDEX_LOGIC", "america1");
map6.put("DESC_CN", "america");
map6.put("DESC_EN", "america");
map6.put("EXT1", "america");
map6.put("EXT2", "america");
map6.put("EXT3", "america");
list1.add(map3);
list1.add(map4);
list2.add(map5);
list2.add(map6);
map.put("Sheet1", list1);
map.put("Sheet2", list2);
for(Entry<String,List<Map<String,String>>> entry : map.entrySet()){
if(entry.getKey().equals("Sheet1")){
int i = 0;
Sheet sheet = xwb.createSheet("Sheet1");
for(Map<String,String> obj : entry.getValue()){
for(Entry<String,String> entry1 : obj.entrySet()){
Row row = sheet.createRow(i);
Cell cell = row.createCell(0);
cell.setCellValue(entry1.getValue());
i++;
}
}
}
if(entry.getKey().equals("Sheet2")){
int i = 0;
Sheet sheet = xwb.createSheet("Sheet2");
Row rowTitle = sheet.createRow(0);
Cell cellTitle1 = rowTitle.createCell(0);
Cell cellTitle2 = rowTitle.createCell(1);
cellTitle1.setCellValue("指标项");
cellTitle2.setCellValue("指标值");
for(Map<String,String> obj : entry.getValue()){
String str = "";
Row row = sheet.createRow(i+1);
Cell cell1 = row.createCell(1);
Cell cell = row.createCell(0);
for(Entry<String,String> entry1 : obj.entrySet()){
if(entry1.getKey().equals("INDEX_LOGIC")){
str = entry1.getValue();
}
if(entry1.getKey().equals("NAME_EN")){
cell.setCellValue(entry1.getValue());
}
if(entry1.getKey().equals("INDEX_TABLE") && entry1.getValue() == "INDEX_VALUE"){
cell1.setCellValue(entry1.getValue());
}
if(entry1.getKey().equals("INDEX_TABLE") && entry1.getValue() != "INDEX_VALUE"){
cell1.setCellValue(entry1.getValue());
Drawing draw = sheet.createDrawingPatriarch();
Comment ct = draw.createCellComment(new XSSFClientAnchor(0, 0, 0, 0,
(short) 3, 3, (short) 5, 6) );
ct.setString(new XSSFRichTextString(str));
cell1.setCellComment(ct);
}
}
i++;
}
}
}
FileOutputStream fileoutput = new FileOutputStream("D:\\new1.xlsx");
xwb.write(fileoutput);
fileoutput.close();
}
public void putValue(Row row , int index,String value){
Cell cell = row.createCell(index);
cell.setCellValue(value);
}
}