怎么筛选execl表中的内容并添加
这是原来的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();
}
catch(Exception e)
{
e.printStackTrace();
}
return list;
}
public static void modifyExcel(File file1,File file2,List<Clothes> list)
{
try
{
Workbook rwb = Workbook.getWorkbook(file1);
WritableWorkbook wwb = Workbook.createWorkbook(file2,rwb);//copy
WritableSheet ws = wwb.getSheet(0);
for(int i=1;i<list.size();i++){
Clothes clo=map.get(list.get(i).getParentId());
WritableCell wc =null;
if(clo!=null){
wc = ws.getWritableCell(1,i+1);
if(wc.getType() == CellType.EMPTY){
Label label=new Label(1,i+1,clo.getProductName());
ws.addCell(label);
}
}
}
wwb.write();
wwb.close();
rwb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//测试
public static void main(String[] args)
{
try
{
List<Clothes> list = ExcelHandleDemo.getClothesList("F:\\ceshi.xls");
// for(Clothes clo : list){
// System.out.println(clo.getId()+" "+clo.getProductName());
// }
ExcelHandleDemo.modifyExcel(new File("F:\\ceshi.xls"),new File("F:\\ceshi.xls"),list);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public class Clothes {
private String id;
private String productName;
private String parentId;
private String parentName;
public Clothes() {
}
public Clothes(String id, String parentId, String parentName,
String productName) {
super();
this.id = id;
this.parentId = parentId;
this.parentName = parentName;
this.productName = productName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getParentName() {
return parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
}