看似很难的东西,只要开始了,就离测试不远了。
只需三个文件, 运行XlsMain文件测试就可以了。
XlsDto.java
package student;
public class XlsDto {
private Integer xkh;
private String xh;
private String xm;
private String yxsmc;
private Integer kch;
private String kcm;
private float cj;
public Integer getXkh() {
return xkh;
}
public void setXkh(Integer xkh) {
this.xkh = xkh;
}
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public String getYxsmc() {
return yxsmc;
}
public void setYxsmc(String yxsmc) {
this.yxsmc = yxsmc;
}
public Integer getKch() {
return kch;
}
public void setKch(Integer kch) {
this.kch = kch;
}
public String getKcm() {
return kcm;
}
public void setKcm(String kcm) {
this.kcm = kcm;
}
public float getCj() {
return cj;
}
public void setCj(float cj) {
this.cj = cj;
}
}
XlsDto2Excel.java
package student;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class XlsDto2Excel {
//get엑셀 데이터
public static void xlsDto2Excel(List xls) throws Exception {
int CountColumnNum = xls.size();
HSSFWorkbook hwb = new HSSFWorkbook();
XlsDto xlsDto = null;
HSSFSheet sheet = hwb.createSheet(“goodsList”);//get시트데이터
HSSFRow firstrow = sheet.createRow(0);
HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
String[] names = new String[CountColumnNum];
names[0] = “넘버”;
names[1] = “성명”;
names[2] = “학원”;
names[3] = “클라스”;
names[4] = “성적”;
for (int j = 0; j < CountColumnNum; j++) {
firstcell[j] = firstrow.createCell((short) j);
firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
}
for (int i = 0; i < xls.size(); i++) {
HSSFRow row = sheet.createRow(i + 1);
xlsDto = xls.get(i);
for (int colu = 0; colu <= 4; colu++) {
HSSFCell xh = row.createCell((short) 0);
xh.setCellValue(xlsDto.getXh());
HSSFCell xm = row.createCell((short) 1);
xm.setCellValue(xlsDto.getXm());
HSSFCell yxsmc = row.createCell((short) 2);
yxsmc.setCellValue(xlsDto.getYxsmc());
HSSFCell kcm = row.createCell((short) 3);
kcm.setCellValue(xlsDto.getKcm());
HSSFCell cj = row.createCell((short) 4);
cj.setCellValue(xlsDto.getCj());
}
}
OutputStream out = new FileOutputStream("Z://test_1.xls");
hwb.write(out);
out.close();
System.out.println("성공!!");
}
}
XlsMain.java
package student;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class XlsMain {
public static void main(String[] args) throws IOException {
XlsMain xlsMain = new XlsMain();
XlsDto xls = null;
List list = xlsMain.readXls();
try {
//get엑셀 데이터
XlsDto2Excel.xlsDto2Excel(list);
} catch (Exception e) {
e.printStackTrace();
}
//검증
for (int i = 0; i < list.size(); i++) {
xls = (XlsDto) list.get(i);
System.out.println(xls.getXh() + ” ” + xls.getXm() + ” “+ xls.getYxsmc() + ” ” + xls.getKcm() + ” “+ xls.getCj());
}
}
private List<XlsDto> readXls() throws IOException {
InputStream is = new FileInputStream("Z://test_1.xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
XlsDto xlsDto = null;
List<XlsDto> list = new ArrayList<XlsDto>();
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}
xlsDto = new XlsDto();
// 循环列Cell
HSSFCell xh = hssfRow.getCell((short) 0);
if (xh == null) {
continue;
}
xlsDto.setXh(getValue(xh));
HSSFCell xm = hssfRow.getCell((short) 1);
if (xm == null) {
continue;
}
xlsDto.setXm(getValue(xm));
HSSFCell yxsmc = hssfRow.getCell((short) 2);
if (yxsmc == null) {
continue;
}
xlsDto.setYxsmc(getValue(yxsmc));
HSSFCell kcm = hssfRow.getCell((short) 3);
if (kcm == null) {
continue;
}
xlsDto.setKcm(getValue(kcm));
HSSFCell cj = hssfRow.getCell((short) 4);
if (cj == null) {
continue;
}
list.add(xlsDto);
}
}
return list;
}
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
return String.valueOf(hssfCell.getNumericCellValue());
} else {
return String.valueOf(hssfCell.getStringCellValue());
}
}
}