这是原来的execl格式
我想变成这样的形式
是怎样做到的?
------解决方案--------------------------------------------------------
下载个jxl.jar包(http://download.csdn.net/detail/huangzebiao007/4949884)就可以了,我说下我的思路,你先把excel中的每一行数据封装成一个对象,存放到一个ArrayList集合,再用个hashMap集合把每行的id,对象当成键值对存起来,修改excel表格时根据map.get(list.get(i).getParentId())获取到该行的父类对象,从而获取到父类对象的产品名,再写入表格就可以了,示例代码你看下下面:
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelHandleDemo
{
private static Map<String,Clothes> map=new HashMap<String,Clothes>();
public ExcelHandleDemo()
{
}
private static List<Clothes> getClothesList(String filePath) {
List<Clothes> list = new ArrayList<Clothes>();
try
{
InputStream is = new FileInputStream(filePath);
Workbook rwb = Workbook.getWorkbook(is);
Sheet st = rwb.getSheet("Sheet1");
for(int i=1;i<st.getRows();i++){
String[] str = new String[5];
for(int j=0;j<st.getColumns();j++){
Cell c = st.getCell(j,i);
//通用的获取cell值的方式,返回字符串
str[j] = c.getContents();
//System.out.println("getCell("+j+","+i+")"+" "+c.getContents());
}
Clothes clothes=new Clothes();
clothes.setId(str[0]);
clothes.setParentName(str[1]);
clothes.setProductName(str[2]);
clothes.setParentId(str[3]);
list.add(clothes);
map.put(str[0],clothes);
}
rwb.close();