java excel 1

 

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

class ExcelNode{
 public String str[] = new String[7];
 public ExcelNode() {
  // TODO Auto-generated constructor stub
 }
}

public class Test2 {

 public HashMap ht = null;
 
 /**
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  Test2 t = new Test2();
  t.map();
  t.work();
  
 }
 
 public void map() throws IOException {
  //ht.clear();
  ht = new HashMap();
  FileInputStream in = new FileInputStream("b.xls");
  POIFSFileSystem fs = new POIFSFileSystem(in);
  HSSFWorkbook wb = new HSSFWorkbook(fs);
  HSSFSheet sheet =  wb.getSheetAt(0);
  for(int i = 0;i<157;++i) {   
   HSSFRow row = sheet.getRow(i);
   String code = "";
   String addr = "";
   //if(row.getCell(0).getCellType()==0) {
   // code = (row.getCell(0).getNumericCellValue()+"");    
   //} else {
    code = row.getCell(0).getStringCellValue();   
   //}
   //if(row.getCell(1).getCellType()==0) {
   // addr = (row.getCell(1).getNumericCellValue()+"");    
   //} else {
    addr = row.getCell(1).getStringCellValue();   
   //}
   ht.put(code, addr);
   //p(code + " " + addr);
  }
 }
 
 public void p(Object o) {
  System.out.println(o.toString());
 }
 
 public void work() throws IOException {
  String filename = "非公党建问题数据";
  FileInputStream in = new FileInputStream("a.xls");
  File file = new File(filename);
  file.mkdir();
  POIFSFileSystem fs = new POIFSFileSystem(in);
  HSSFWorkbook wb = new HSSFWorkbook(fs);
    
  //HSSFCellStyle cs = wb.createCellStyle();
  //cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
  HSSFSheet sheet =  wb.getSheetAt(0);
  
 
  String index = "";
  String curindex = "";
  boolean flag = true;
  List<ExcelNode> list = new ArrayList<ExcelNode>();
  for(int i = 0;i<=sheet.getLastRowNum();++i) {   
   HSSFRow row = sheet.getRow(i);
   
   if(row.getCell(0).getCellType()==0) {
    curindex = (row.getCell(0).getNumericCellValue()+"").substring(0, 6);
   } else {
    System.out.println(row.getCell(0).getStringCellValue());
    if(row.getCell(0).getStringCellValue().length()<6){ flag = false; }
    else curindex = (row.getCell(0).getStringCellValue().substring(0, 6));    
   }
   if(!flag) {
    String path = filename+ "/"+ht.get(index) +index + filename+".xls";
    output(list,path);
    break;
   }
   if(!index.equals(curindex) && i!=sheet.getLastRowNum()) {    
    String path = filename+ "/"+ ht.get(index) +index + filename+".xls";
    output(list,path);
    list.clear();
    index = curindex;
   }
   ExcelNode node = new ExcelNode();
   for(int j=0;j<5;++j) {//row.getLastCellNum()
    HSSFCell cell = row.getCell(j);
    if(cell.getCellType()==0) {
     node.str[j] = cell.getNumericCellValue()+"";
    }else {
     node.str[j] = cell.getStringCellValue();
    }    
   }
   node.str[5] = (String) ht.get(index);
   list.add(node);
   if(i >= sheet.getLastRowNum() ) {
    //index = curindex;
    String path = filename+ "/"+ht.get(index) +index + filename+".xls";
    output(list,path);
    list.clear();
    break;
   }
   
  }
 }
 
 
 public void output(List<ExcelNode> list,String path) throws IOException {
  HSSFWorkbook wb = new HSSFWorkbook();
  // create a new sheet
  HSSFSheet sheet = wb.createSheet();
  // declare a row object reference
  HSSFRow row = null;
  // declare a cell object reference
  HSSFCell cell = null;
  
  HSSFCellStyle cs = wb.createCellStyle();
  cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
  
  row = sheet.createRow(0);
  row.createCell(0).setCellValue("注册号");
  row.createCell(1).setCellValue("企业类型");
  row.createCell(2).setCellValue("党员数");
  row.createCell(3).setCellValue("从业人员数");
  row.createCell(4).setCellValue("异常数据说明");
  row.createCell(5).setCellValue("所属局");
  for(int i = 1;i<=list.size();++i) {
   row = sheet.createRow(i);
   for(int j = 0;j<6;++j) {
    row.createCell(j).setCellValue(list.get(i-1).str[j]);
   }
  }
  
  // Save
  FileOutputStream out = new FileOutputStream(path);
  wb.write(out);
  out.close();
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值