用MyEclipse写的java代码,从一个excel文件中读数据,后写入另一excel文件,运行后出错,请帮帮我~
源代码如下:packagest.catv.core.testzg;importjava.io.File;importjxl.Sheet;importjxl.Workboo...
源代码如下:
package st.catv.core.testzg;
import java.io.File;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ReadExcel {
public static void main(String[] args) {
Workbook fromWorkbook = null;
WritableWorkbook toWorkbook = null;
try {
File fromFile = new File("E:/from.xls");// 源文件
File toFile = new File("E:/to.xls");// 目标文件
toFile.createNewFile();// 创建目标文件
fromWorkbook = Workbook.getWorkbook(fromFile);
toWorkbook = Workbook.createWorkbook(toFile);
Sheet fromSheet = fromWorkbook.getSheet(0);
WritableSheet toSheet = toWorkbook.createSheet("sheet1", 0);
// 设置样式
WritableCellFormat wcf = new WritableCellFormat(new WritableFont(
WritableFont.ARIAL, 10, WritableFont.BOLD));
toSheet.setColumnView(2, 50);// 设置列的宽度(详细地址)
toSheet.addCell(new Label(0, 0, "编号", wcf));
toSheet.addCell(new Label(1, 0, "用户证号", wcf));
toSheet.addCell(new Label(2, 0, "详细地址", wcf));
int rows = fromSheet.getRows();// 行数
// 从第二行开始读取
for (int i = 1; i < rows; i++) {
String number = fromSheet.getCell(0, i).getContents();// 编号
String userno = fromSheet.getCell(1, i).getContents();// 用户证号
String oldaddress = fromSheet.getCell(2, i).getContents();// 详细地址
toSheet.addCell(new Label(0, i, number));
toSheet.addCell(new Label(1, i, userno));
toSheet.addCell(new Label(2, i, oldaddress));
}
toWorkbook.write();
} catch (Exception e) {
e.printStackTrace();
} finally {
fromWorkbook.close();
try {
toWorkbook.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("ok");
}
}
}
运行后出现:
Command format: Demo [-unicode] [-csv] [-hide] excelfile
Demo -xml [-format] excelfile
Demo -readwrite|-rw excelfile output
Demo -biffdump | -bd | -wa | -write | -formulas | -features excelfile
Demo -ps excelfile [property] [output]
Demo -version | -logtest | -h | -help
经过我师兄的检查,我最终把问题解决了。犯了两个错误:1.如1楼所说,命令格式写错了(这是相当2的了,呵呵);2.没有加载相应的包(我是菜鸟)。
多谢各位了~ 展开
package st.catv.core.testzg;
import java.io.File;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ReadExcel {
public static void main(String[] args) {
Workbook fromWorkbook = null;
WritableWorkbook toWorkbook = null;
try {
File fromFile = new File("E:/from.xls");// 源文件
File toFile = new File("E:/to.xls");// 目标文件
toFile.createNewFile();// 创建目标文件
fromWorkbook = Workbook.getWorkbook(fromFile);
toWorkbook = Workbook.createWorkbook(toFile);
Sheet fromSheet = fromWorkbook.getSheet(0);
WritableSheet toSheet = toWorkbook.createSheet("sheet1", 0);
// 设置样式
WritableCellFormat wcf = new WritableCellFormat(new WritableFont(
WritableFont.ARIAL, 10, WritableFont.BOLD));
toSheet.setColumnView(2, 50);// 设置列的宽度(详细地址)
toSheet.addCell(new Label(0, 0, "编号", wcf));
toSheet.addCell(new Label(1, 0, "用户证号", wcf));
toSheet.addCell(new Label(2, 0, "详细地址", wcf));
int rows = fromSheet.getRows();// 行数
// 从第二行开始读取
for (int i = 1; i < rows; i++) {
String number = fromSheet.getCell(0, i).getContents();// 编号
String userno = fromSheet.getCell(1, i).getContents();// 用户证号
String oldaddress = fromSheet.getCell(2, i).getContents();// 详细地址
toSheet.addCell(new Label(0, i, number));
toSheet.addCell(new Label(1, i, userno));
toSheet.addCell(new Label(2, i, oldaddress));
}
toWorkbook.write();
} catch (Exception e) {
e.printStackTrace();
} finally {
fromWorkbook.close();
try {
toWorkbook.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("ok");
}
}
}
运行后出现:
Command format: Demo [-unicode] [-csv] [-hide] excelfile
Demo -xml [-format] excelfile
Demo -readwrite|-rw excelfile output
Demo -biffdump | -bd | -wa | -write | -formulas | -features excelfile
Demo -ps excelfile [property] [output]
Demo -version | -logtest | -h | -help
经过我师兄的检查,我最终把问题解决了。犯了两个错误:1.如1楼所说,命令格式写错了(这是相当2的了,呵呵);2.没有加载相应的包(我是菜鸟)。
多谢各位了~ 展开
3个回答
展开全部
我没仔细看,感觉是你的路径有问题,应该用\吧
File fromFile = new File("E:/from.xls");// 源文件
File toFile = new File("E:/to.xls");// 目标文件
你改成这样试试
File fromFile = new File("E:\\from.xls");// 源文件
File toFile = new File("E:\\to.xls");// 目标文件
File fromFile = new File("E:/from.xls");// 源文件
File toFile = new File("E:/to.xls");// 目标文件
你改成这样试试
File fromFile = new File("E:\\from.xls");// 源文件
File toFile = new File("E:\\to.xls");// 目标文件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你 这个toFile.createNewFile();// 创建目标文件的方法貌似不对,应该用工具进行创建,而不是这样直接创建。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |