使用哈希映射写入到Excel文件 [英] Writing to Excel File using hash map
本文介绍了使用哈希映射写入到Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 我是从哈希映射得到确切值,但我的Apache POI行和
细胞不能设置值正确预期的结果一样,请让我
know.Thanks
我得到的结果哈希映射这样的:
I'm getting result hash-map like that:
{1=[ACSS Description1, ACSS Description2, ACSS Description3, SACSS Description4], 2=[11, 1, 4, 12]}
我期待:
我得到的结果基于以下code:
I'm getting result based on below code :
这就是我的code:
public void getList(List<ExportReport> listcriteria)
{
Map<Integer, List<String>> hashmap = new HashMap<Integer , List<String>>();
List<String> listpropertyvalue =new ArrayList<String>();
for(int i=0; i < listcriteria.size(); i++)
{
String strValue =listcriteria.get(i).getDescription();
listpropertyvalue.add(strValue);
hashmap.put(1, listpropertyname);
}
listpropertyvalue =new ArrayList<String>();
for(int i=0;i<listcriteria.size();i++){
String strInterValue=listcriteria.get(i).getExportIntervalId().toString();
listpropertyvalue.add(strInterValue);
hashmap.put(2, listpropertvalue);
}
}
Set<Integer> keyset = hashmap.keySet();
int rownum = 1;
int cellnum = 0
for(Integer key : keyset){
Row row = worksheet.createRow(rownum++);
Cell cell = row.createCell(cellnum);
List<String> nameList = hashmap.get(key);
for(Object obj : nameList)
{
if(obj instanceof Date)
{
cell.setCellValue((Date) obj);
}
else if(obj instanceof Boolean)
{
cell.setCellValue((Boolean) obj);
}
else if(obj instanceof String)
{
cell.setCellValue((String) obj);
}
else if(obj instanceof Double)
{
cell.setCellValue((Double) obj);
}
}
cellnum++;
rownum=1;
}
}
我在做什么错了?
What am I doing wrong?
推荐答案
很抱歉这么晚答复。
您使用code,我创建了一个初始化Excel文件Writesheet.xlsx 5排每排有5个单元的工作程序。另外,我创建了一个包含5字符串的列表。然后我用的GetList(列表&LT; ExportReport&GT; listcriteria)
方法写入名单上Writesheet.xlsx含量
Using you code, I created a working program that initialize a Excel file Writesheet.xlsx with 5 rows each row contains 5 Cells. Also, I created a List that contains 5 strings. Then I used getList(List<ExportReport> listcriteria)
method to write the content of this List on Writesheet.xlsx
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Test {
static XSSFWorkbook workbook = new XSSFWorkbook();
public void getList(List<String> listcriteria){
Map<Integer, List<String>> hashmap = new HashMap<Integer , List<String>>();
//create 5 key value pairs
for(int i=0; i < 5; i++){
hashmap.put(i, listcriteria);
}
System.out.println("hashmap : "+hashmap);
Set<Integer> keyset = hashmap.keySet();
int rownum = 0;
int cellnum = 0;
XSSFSheet sheet = workbook.getSheetAt(0);
rownum = 0;
for(Integer key : keyset){
List<String> nameList = hashmap.get(key);
for(String s : nameList){
XSSFRow row = sheet.getRow(rownum++);
Cell cell = row.getCell(cellnum);
if(null!=cell){
cell.setCellValue(s);
}
}
cellnum++;
rownum=0;
}
}
public static void main(String[] args) throws IOException {
//Creation of List from an Array to test getList Method
String[] ss = {"a","b","c","d","e"};
List<String> listcriteria = new ArrayList<String>();
listcriteria.addAll(Arrays.asList(ss));
/***********************************************************/
Test t = new Test();
// Because I put 5 key values pairs in hashmap (see getList method), I create Writesheet.xlsx
// file that contains 5 rows each row contains 5 cell
FileOutputStream out = new FileOutputStream( new File("Writesheet.xlsx"));
XSSFSheet sheet = workbook.createSheet();
for(int i = 0;i<5;i++){
XSSFRow row = sheet.createRow(i);
for(int j=0;j<5;j++)
row.createCell(j);
}
workbook.write(out);
out.close();//end creation of Excel file
// I open Writesheet.xlsx file and write the data on it
InputStream inp = new FileInputStream( new File("Writesheet.xlsx"));
workbook = new XSSFWorkbook(inp);
// listcriteria contains the data that will be written it on Writesheet.xlsx
t.getList(listcriteria);
out = new FileOutputStream( new File("Writesheet.xlsx"));
workbook.write(out);
out.close();
inp.close();
System.out.println("Writesheet.xlsx written successfully" );
}
}
这篇关于使用哈希映射写入到Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文