java 复制excel jxl_java中使用jxl的jar包处理excel的复制,更新等问题。

前几个月,和老师一起整理数据,需要处理excel表格。就在网上找了jxl.jar。使用中遇到了一些问题。下面和大家分享一下。

其中一部分问题是把excel中的一列(id)变成7位数的id。(比如,原来id是4,excel的名称是127,新的excel中id要变成1270004.id是56,就要变成1270056)。其他列不变。

package datainput3;

import java.io.File;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import jxl.Cell;

import jxl.Sheet;

import jxl.Workbook;

import jxl.read.biff.BiffException;

import java.io.File;

import java.io.IOException;

import jxl.Workbook;

import jxl.write.Label;

import jxl.write.Number;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import jxl.write.WriteException;

import jxl.write.biff.RowsExceededException;

import java.io.File;

import java.io.IOException;

import jxl.Workbook;

import jxl.read.biff.BiffException;

import jxl.write.Label;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import jxl.write.WriteException;

import jxl.write.biff.RowsExceededException;

public class Main {

//excel转七位

public static void main(String[] args) throws IOException, RowsExceededException, WriteException{

// TODO Auto-generated method stub

String nine = new String();

String seven = new String();

String goal = new String();

String name = new String();

nine = "127";//原excel的名字

seven = "E:\\wenwu\\access转excel";//原excel的存贮位置

goal = "E:\\wenwu\\excel转7位excel";//改变id后excel的存贮位置

name = "127";//改变id后Excel的名字

try{

//打开空的excel

Workbook wb = Workbook.getWorkbook(new File(goal + "\\" + name + ".xls"));

//打开文件的一个副本,并且指定数据写回到原文件

WritableWorkbook workbook = Workbook.createWorkbook(new File(goal + "\\" + name + ".xls"),wb);

//打开两张sheet,重命名为Material_Info,Material_Index

WritableSheet sheet3 = workbook.createSheet("Material_Info", 0);

WritableSheet sheet4 = workbook.createSheet("Material_Index", 1);

String str3 = new String();

str3 = nine;

int intstr3 = Integer.valueOf(str3).intValue();

//打开原excel

File f = new File(seven + "\\" + nine + ".xls");

try {

Workbook book = Workbook.getWorkbook(f);// 打开3excel表格

Sheet sheet1 = book.getSheet(0); // 获得第一个工作表对象

Sheet sheet2 = book.getSheet(1); // 获得第2个工作表对象

Cell cell7 = sheet1.getCell(0, 0);//获取该sheet的第0行第0列 即excel的第一行第A列

//将该单元格写入到写到目标文件的第0行0列

Label label11 = new Label(0,0,cell7.getContents());

sheet3.addCell(label11);

Cell cell8 = sheet2.getCell(0, 0);

Label label12 = new Label(0,0,cell8.getContents());

sheet4.addCell(label12);

//下面是对第一列的id修改工作

for(int k = 1;k < sheet1.getRows();k++)

{

Cell cell = sheet1.getCell(0, k); // 获得单元格

String strstr3 = new String();

strstr3 = cell.getContents();

int intstr31 = Integer.valueOf(strstr3).intValue();

int intstr32 = 10000*intstr3 + intstr31;

String str33 = Integer.toString(intstr32);

Label label1 = new Label(0,k,str33);

sheet3.addCell(label1);

}

for(int k = 1;k < sheet2.getRows();k++)

{

Cell cell6 = sheet1.getCell(0, k); // 获得单元格

String strstr3 = cell6.getContents();

int intstr31 = Integer.valueOf(strstr3).intValue();

int intstr32 = 10000*intstr3 + intstr31;

String str33 = Integer.toString(intstr32);

Label label6 = new Label(0,k,str33);

sheet4.addCell(label6);

}

//下面是对除第一列以外,别的列的复制工作

for (int i = 0; i < sheet1.getRows(); i++) {

for(int j = 1;j < sheet1.getColumns();j++)

{

Cell cell1 = sheet1.getCell(j, i); // 获得单元格

Label label1 = new Label(j,i,cell1.getContents());

sheet3.addCell(label1);

}

}

for (int i = 0; i < sheet2.getRows(); i++) {

for(int j = 1;j < sheet2.getColumns();j++)

{

Cell cell2 = sheet2.getCell(j, i); // 获得单元格

Label label2 = new Label(j,i,cell2.getContents());

sheet4.addCell(label2);

}

}

}catch (BiffException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//执行写入到文件,并关闭

workbook.write();

workbook.close();

}

catch (BiffException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

遇到的问题,

1  jxl的jar包只能操作03版的后缀名为.xls的excel,不能处理更高版的.xlsx的excel。

2  对目标excel进行操作失误失,要把目标excel删除,然后新建一个excel表格,在对其操作。为什么要这样呢?因为我们新建一个空的excel(我自己的电脑上)是18kb,使用         java程序操作失败后,也是空的,就变成了0kb,在进行操作,java就会报出error,找不到该excel,此时,删除0kb的excel,重新新建一个,即可。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值