利用JXL根据Exel的某一列进行拆分Excel

请不要在意类名
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
import org.omg.CORBA.Any;
import org.omg.CORBA.Object;
import org.omg.CORBA.TypeCode;
import org.omg.CORBA.portable.InputStream;

import jxl.CellView;
import jxl.Range;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.write.*;
import jxl.write.biff.WritableWorkbookImpl;

public class POIExcelTransform {
 private static final HashMap String = null;
 public static String tt="E:/WORKHome/";
 public POIExcelTransform() {
  // TODO Auto-generated constructor stub
 }

 

 /*************
  * 我的第一次尝试
  */
 public void copyExcel(){
  String whole_path=tt;
  String wholeName="待确认工作量-";
  Map<String, Integer> nameMap = new HashMap<String, Integer>();
  Map<String, WritableWorkbook> WritableWorkbookMap = new HashMap<String, WritableWorkbook>();
  try {
   Workbook Workbook_sourc  = Workbook.getWorkbook(new File(tt+"确认1.xls"));
   Workbook Workbook_model  = Workbook.getWorkbook(new File(tt+"确认1_mode.xls"));
   File excelFile = new File(tt+"test11.xls");
   //OutputStream tOutputStream = new FileOutputStream(excelFile);
   
   Sheet tSheet =Workbook_sourc.getSheet(0);
   int i_row=2;
   int j_column=0;
   int name_i=18;
   String curString="";
   String nextString="";
   WritableCellFormat cf = new WritableCellFormat(NumberFormats.TEXT);
   cf.setBorder(Border.ALL, BorderLineStyle.THIN); 
   cf.setWrap(true);
   
   //行循环
   while(true){
    curString =tSheet.getCell(name_i, i_row).getContents();
    nextString=tSheet.getCell(name_i, i_row+1).getContents();;
    if(curString==null||"".equals(curString)){
     break;
    }
    File toFile = new File(whole_path+wholeName+curString+".xls");
    WritableWorkbook workbook_comfirm;
    if(!toFile.exists()){
     //不存在
     WorkbookSettings settings = new WorkbookSettings();
     settings.setWriteAccess(null);
     workbook_comfirm = Workbook.createWorkbook(toFile,Workbook_model,settings);
     WritableSheet tttsheet = workbook_comfirm.getSheet(0);
     tttsheet.getSettings().setDefaultRowHeight(100);
     tttsheet.getSettings().setDefaultColumnWidth(10);;
     for(int i=0;i<19;++i){
      Label label = new Label(i, 2, tSheet.getCell(i, i_row).getContents());
      
      label.setCellFormat(cf);
      // 将定义好的单元格添加到工作表中
      tttsheet.addCell(label);
      
     }
     WritableWorkbookMap.put(curString, workbook_comfirm);
     nameMap.put(curString, 2);
     //workbook_comfirm.write();
    // workbook_comfirm.close();
     //workbook_comfirm=null;
     //workbook_comfirm.write();
    }else{
     
     
     //存在
     workbook_comfirm=WritableWorkbookMap.get(curString);
     Integer lastInt=nameMap.get(curString);
     nameMap.put(curString, lastInt+1);
     
     WritableSheet tttsheet = workbook_comfirm.getSheet(0);
     for(int i=0;i<19;++i){
      Label label = new Label(i, lastInt+1, tSheet.getCell(i, i_row).getContents());
      // 将定义好的单元格添加到工作表中
      label.setCellFormat(cf);
      tttsheet.addCell(label);
      
     }
    
     //workbook_comfirm.write();
//     workbook_comfirm.write();
//     
//     workbook_comfirm.close();
//     workbook_comfirm=null;
    }
    i_row++;
//    if(!curString.equals(nextString)){
//     workbook_comfirm.write();
     
//     workbook_comfirm.close();
//     workbook_comfirm=null;
//     System.gc();
//    }
   }
   
   Collection<WritableWorkbook> coWritableWorkbook= WritableWorkbookMap.values();
   Iterator<WritableWorkbook> itWritableWorkbook=coWritableWorkbook.iterator();
   while(itWritableWorkbook.hasNext()){
    WritableWorkbook WritableWorkbookworkbook_comfirm =itWritableWorkbook.next();
    WritableWorkbookworkbook_comfirm.write();
    WritableWorkbookworkbook_comfirm.close();
    WritableWorkbookworkbook_comfirm=null;
    System.gc();
   }
  
   Workbook_sourc.close();
   

  } catch (Exception e) {
   e.printStackTrace();
  }
 
 }
 public static void main(String[] args){
  try{
   System.out.println("!!!!!!!!!!!!!!");
   new POIExcelTransform().copyExcel();
   System.out.println("~~~~~~");
  }catch(Exception e){
   e.printStackTrace();
  }
  
  
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值