乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > java怎么把excel中的数据加入到list里面-list转excel,excel中怎样做list

java怎么把excel中的数据加入到list里面-list转excel,excel中怎样做list

作者:乔山办公网日期:

返回目录:excel表格制作


主要是zhidaoresponse的输出流里面是你的内容。
给你一个我之前写的下载文件的例子,你参考试试看
rout = response.getOutputStream();
is=new BufferedInputStream(new FileInputStream(filePath));
os = new BufferedOutputStream(rout);
byte [] buff = new byte[2048];
int byteRead;
while(-1!=(byteRead = is.read(buff, 0, buff.length)))
{
os.write(buff,0,byteRead);
}
最后关闭流。return null

提供一个不用poi或者jxl的方法,步骤:
1. 把excel另存为.txt文档
2 .然后用BufferReader一行行的读

例:
File file = new File("C:\\test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String cityStr = null ;
List<String> list= new ArrayList<String>();
while((cityStr = br.readLine()) != null)
{
list.add(cityStr);
}
先按行读取,然后获取列,定位到单元格后就可以获取值了,循环存入list即可。
/**
* 读取Excel数据内容
* @param InputStream
* @return Map 包含单元格数据内容的Map对象
*/
public Map<Integer, String> readExcelContent(InputStream is) {
Map<Integer, String> content = new HashMap<Integer, String>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < colNum) {
// 每个单元格的数据内容用"-"分割开e79fa5e98193e4b893e5b19e361,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
str += getCellFormatValue(row.getCell((short) j)).trim() + " ";
j++;
}
content.put(i, str);
str = "";
}
return content;
}

首先这个List尽量是Object类型的,也就是:List<Object[]> list
其次就是开始遍历这个list
int currentRowNum = startRowNum-1;//从第几行开始写
int currentColNum = startColNum-1;//从第几列开始写
for (Object[] objects : list) {//遍历list
XSSFRow row = sheet.createRow(currentRowNum);//一个list就是一行,创建e799bee5baa6e78988e69d83365一个行
for (Object object : objects) {//遍历每一个list的Object[]数组
if (object==null) {
XSSFCell cell = row.createCell(currentColNum++, XSSFCell.CELL_TYPE_BLANK);//如果该单元格为空,则创建一个空格子,不然数据会移位
cell.setCellValue("");//将这个空格设置为空字符串
}else if(object instanceof Double || object instanceof Float){//判断这个格子放的数据的类型,其他的同理
XSSFCell cell = row.createCell(currentColNum++, XSSFCell.CELL_TYPE_NUMERIC);//如果是Double或者Float类型的,则用这个方式
cell.setCellValue(Double.parseDouble(object.toString()));
}else if(object instanceof Long || object instanceof Integer){
XSSFCell cell = row.createCell(currentColNum++, XSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(Double.parseDouble(object.toString()));
}else if(object instanceof Date){
XSSFCell cell = row.createCell(currentColNum++, XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new SimpleDateFormat("dd-MM-yyyy").format((Date)object));
}else if(object instanceof Boolean){
XSSFCell cell = row.createCell(currentColNum++, XSSFCell.CELL_TYPE_BOOLEAN);
cell.setCellValue((Boolean)object);
}else{
XSSFCell cell = row.createCell(currentColNum++, XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(String.valueOf(object));
}
}
currentRowNum++;//写完第一个list,跳到下一行
currentColNum = startColNum-1;
}

这种是poi操作EXCEL的方法哈。。
jxl操作略有不同。

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网