使用POI操作Excel的代码(二)

2014-11-24 01:08:56 · 作者: · 浏览: 1
eetName, HSSFSheet sh) {
this.name = sheetName;
this.sheet = sh;
sheet.setDisplayGridlines(true);
}

public ExcelSheet(String sheetName, HSSFSheet sh,HSSFCellStyle titleStyle) {
this.name = sheetName;
this.sheet = sh;
this.titleStyle=titleStyle;
}

// public ExcelSheet(){
// this(name);
// }

public void addRecord(String[] record) {
if(header!=null){
if(header.length!=record.length){
return;
}
}

fillContent(record,sheet.getLastRowNum()+1,null);

}

public String[] getHeader() {
return this.header;
}

public void setHeader(String[] header) {
this.header=header;
fillContent(header,0,this.titleStyle);
}

private void fillContent(String[] crow, int rowNum, HSSFCellStyle style) {
HSSFRow row = sheet.createRow((short) rowNum);

for (int i = 0; i < crow.length; i++) {
String s = crow[i];
HSSFCell cell = row.createCell((short) i);
if(style!=null){
cell.setCellStyle(style);
}
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(s);
}
}

public void setValue(int rowNum,int colNum,String value){
HSSFRow row=this.sheet.getRow(rowNum);
HSSFCell cell=row.getCell((short)colNum);
if (cell==null){
cell = row.createCell((short) colNum);
}
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(value);
}

public String getName() {
// TODO Auto-generated method stub
return this.name;
}

public void addRecord(ArrayList arr) {
for (String[] row : arr) {
this.addRecord(row);
}

}

public List getRecords(){
ArrayList vs=new ArrayList();
for (int j = 0; j < this.sheet.getLastRowNum(); j++) {
HSSFRow row=this.sheet.getRow(j);
ArrayList cellsStr = new ArrayList();
for (short k = 0; k < row.getLastCellNum(); k++) {
HSSFCell cell=row.getCell(k);
if(cell!=null){
cellsStr.add(cell.getStringCellValue());
}
}
System.out.println("row=