java code about POI test

packagecom.king_w.test;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.OutputStream;

importjava.io.PrintWriter;

importjava.util.Set;

importorg.apache.poi.ss.usermodel.Cell;

importorg.apache.poi.ss.usermodel.CellStyle;

importorg.apache.poi.ss.usermodel.CreationHelper;

importorg.apache.poi.ss.usermodel.DataFormat;

importorg.apache.poi.ss.usermodel.Font;

importorg.apache.poi.ss.usermodel.Row;

importorg.apache.poi.ss.usermodel.Sheet;

importorg.apache.poi.ss.usermodel.Workbook;

importorg.apache.poi.xssf.usermodel.XSSFWorkbook;

publicclassPOItest{

publicstaticvoidmain(String[]args){

Filefile=newFile("C:\\software\\data.xlsx");

Filefile1=newFile("c:\\software\\data.txt");

InputStreamis;

try{

//output

Workbookwb1=newXSSFWorkbook();

CreationHelperhelper=wb1.getCreationHelper();

Sheetsheet1=wb1.createSheet("Sheet_1");

OutputStreamos=newFileOutputStream(newFile("c://software//datacopy.xlsx"));

CellStylecs=wb1.createCellStyle();

DataFormatdf=wb1.createDataFormat();

Fontf=wb1.createFont();

Fontf2=wb1.createFont();

//setfont1to12pointtype

f.setFontHeightInPoints((short)12);

//makeitblue

f.setColor((short)0xc);

//makeitbold

//arialisthedefaultfont

f.setBoldweight(Font.BOLDWEIGHT_BOLD);

//setfont2to10pointtype

f2.setFontHeightInPoints((short)10);

//makeitred

f2.setColor((short)Font.COLOR_RED);

//makeitbold

f2.setBoldweight(Font.BOLDWEIGHT_BOLD);

f2.setStrikeout(true);

//setcellstlye

cs.setFont(f);

//setthecellformat

cs.setDataFormat(df.getFormat("#,##0.0"));

//setathinborder

cs.setBorderBottom(cs.BORDER_THIN);

//fillwfgfillcolor

cs.setFillPattern((short)CellStyle.SOLID_FOREGROUND);

//setthecellformattotextseeDataFormatforafulllist

//cs.setDataFormat(XSSFDataFormat.getBuiltinFormat("text"));

//setthefont

cs.setFont(f2);

//readthefile

PrintWriterpw=newPrintWriter(file1);

is=newFileInputStream(file);

XSSFWorkbookwb=newXSSFWorkbook(is);

Sheetsheet=wb.getSheetAt(0);

Stringtemp_locate=null;

Stringlocate=null;

Stringtemp_corpname=null;

Stringcorpname=null;

Stringmoney=null;

Stringtemp_code=null;

Stringcode=null;

doublemon=0;

inti=0;

Set<String>s=null;

booleanflag=false;

for(Rowrow:sheet){

Cellcell0=row.getCell(0);

Cellcell1=row.getCell(1);

Cellcell2=row.getCell(2);

Cellcell3=row.getCell(3);

corpname=cell1.getStringCellValue();

locate=cell0.getStringCellValue();

code=cell2.getStringCellValue();

money=cell3.getStringCellValue();

//System.out.println("code:"+code+"money:"+money);

if(temp_corpname==null){

temp_corpname=corpname;

mon=Double.parseDouble(money);

//System.out.println("tempnameisnull");

continue;

}

if(temp_corpname!=null&&corpname.equals(temp_corpname))

{

temp_code=code;

temp_locate=locate;

//System.out.println("notnull,butcorpnameisequaltotempname");

mon+=Double.parseDouble(money);

continue;

}

if(temp_corpname!=null&&!corpname.equals(temp_corpname)){

//putthemonintonewtable

s.add(temp_corpname);

if(mon>=300000.0)

{

Rowrow1=sheet1.createRow(i);

Cellcell_a=row1.createCell(0);

Cellcell_b=row1.createCell(1);

Cellcell_c=row1.createCell(2);

Cellcell_d=row1.createCell(3);

Cellcell_e=row1.createCell(4);

System.out.println(temp_locate+""+temp_corpname+""+temp_code+""+mon);

cell_a.setCellValue(temp_locate);

cell_b.setCellValue(temp_corpname);

cell_c.setCellValue(temp_code);

cell_d.setCellValue(mon);

cell_e.setCellValue("2901");

//pw.println(temp_locate);

//pw.println(temp_corpname);

//pw.println(temp_code);

//pw.println(mon);

i++;

}

mon=Double.parseDouble(money);

}

temp_corpname=corpname;

temp_code=code;

temp_locate=locate;

}

//pw.flush();

//pw.close();

wb1.write(os);

os.close();

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}

}

相关推荐