package totalExcel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.Number;
import jxl.write.Boolean;
public class ExportExcel {
public static void main(String[] args) throws Exception{
String path="E:/excels";
List<String> resultFileName=new ArrayList<String>();
ergodic(new File(path), resultFileName);
//写出totalData.xls文件
OutputStream os = null;
WritableWorkbook wwb = null;
try {
String newFliePath = "E:/Test.xls";
File file = new File(newFliePath);
if (!file.isFile())// 如果指定文件不存在,则新建该文件
file.createNewFile();
os = new FileOutputStream(file);// 创建一个输出流
wwb = Workbook.createWorkbook(os);
WritableSheet sheet1 = wwb.createSheet("sheet1", 0);// 创建一个工作页,第一个参数的页名,第二个参数表示该工作页在excel中处于哪一页
// 第一个参数表示列,第二个参数表示行
Label label00 = new Label(0, 0, "姓名");// 填充第一行第一个单元格的内容
Label label01=new Label(1,0,"工号");
Label label02=new Label(2, 0, "日期");
sheet1.addCell(label00);
sheet1.addCell(label01);
sheet1.addCell(label02);
//读取文件
int count=0;//记录文件个数
for (String filePath : resultFileName) {
// System.out.println("-------");
//打开文件
Workbook book = Workbook.getWorkbook(new File(filePath));
//获得第一个工作表对象
Sheet sheet = book.getSheet(0);
//获得第一列第二行的单元格
Cell cell01 = sheet.getCell(0, 1);
Cell cell11 = sheet.getCell(1, 1);
Cell cell21 = sheet.getCell(2, 1);
String result01 = cell01.getContents();//根据下标取得第一列第一行数据
String result11 = cell11.getContents();
String result21 = cell21.getContents();
// Date date =new Date(result21);
if(cell21.getType() == CellType.DATE){
DateCell dc = (DateCell)cell21;
result21 = new SimpleDateFormat("yyyy/MM/dd").format(dc.getDate());
}
// System.out.println(result01+result11+result21);
Label labeln0=new Label(0,1+count,result01);
Label labeln1=new Label(1, 1+count, result11);
Label labelDT = new Label(2, 1+count, result21); // 添加日期
sheet1.addCell(labeln0);
sheet1.addCell(labeln1);
sheet1.addCell(labelDT);
count++;
}
wwb.write();// 将内容写到excel文件中
os.flush();// 清空输出流
}catch(Exception e){
}finally {
try {
if (wwb != null)
wwb.close();
if (os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
/**
* 在用jxl解析excle的时候,如果excel中有小数,
* 如果小数点后的位数多的化,可能会用科学技术法来显示结果,
* 为了避免显示科学计数法,可以通过两个办法来解决
* 1:将excel的小数的cell格的格式改成字符串型。
* 2:用下面的程序处理。
*/
// Cell cel = sheet.getCell(1,0);
// String data = cel.getContents();
// if(cel.getType() == CellType.DATE){
// DateCell dc = (DateCell)cel;
// data = new SimpleDateFormat("YYYY-MM-DD").format(dc.getDate());
// }else if(cel.getType() == CellType.NUMBER || cel.getType() == CellType.NUMBER_FORMULA){
// NumberCell nc = (NumberCell) cel;
// data = ""+nc.getValue();
// }
/* 写出文件 */
/* OutputStream os = null;
WritableWorkbook wwb = null;
try {
String newFliePath = "E:/excels/Test.xls";
File file = new File(newFliePath);
if (!file.isFile())// 如果指定文件不存在,则新建该文件
file.createNewFile();
os = new FileOutputStream(file);// 创建一个输出流
wwb = Workbook.createWorkbook(os);
WritableSheet sheet1 = wwb.createSheet("sheet1", 0);// 创建一个工作页,第一个参数的页名,第二个参数表示该工作页在excel中处于哪一页
// 第一个参数表示列,第二个参数表示行
Label label = new Label(0, 0, "姓名");// 填充第一行第一个单元格的内容
sheet1.addCell(label);
WritableFont wf = new WritableFont(WritableFont.TIMES, 18,
WritableFont.BOLD, true);
WritableCellFormat wcf = new WritableCellFormat(wf);
label = new Label(0, 1, "font", wcf);// 定制单元格格式
sheet1.addCell(label);
NumberFormat nf = new NumberFormat("#.###");// 格式化数字
wcf = new WritableCellFormat(nf);
Number nb = new Number(0, 2, 3.1415926, wcf);
sheet1.addCell(nb);
Boolean labelB = new Boolean(0, 3, false); // 填充布尔型数据
sheet1.addCell(labelB);
DateTime labelDT = new DateTime(0, 4, new java.util.Date()); // 添加日期
sheet1.addCell(labelDT);
DateFormat df = new DateFormat("yyyy-MM-dd hh:mm:ss"); // 添加时间
WritableCellFormat wcfDF = new WritableCellFormat(df);
DateTime labelDTF = new DateTime(0, 5, new java.util.Date(),
wcfDF);
sheet1.addCell(labelDTF);
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 20,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.GREEN);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
wcfFC.setBackground(jxl.format.Colour.RED);// 设置单元格的颜色为红色
label = new jxl.write.Label(0, 6, "i love china", wcfFC);
sheet1.addCell(label);
wwb.write();// 将内容写到excel文件中
os.flush();// 清空输出流
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} finally {
try {
if (wwb != null)
wwb.close();
if (os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}*/
}
/**
* 递归调用获取文件夹下所有文件名
* @param file
* @param resultFileName
* @return
*/
private static List<String> ergodic(File file,List<String> resultFileName){
File[] files = file.listFiles();
if(files==null)return resultFileName;// 判断目录下是不是空的
for (File f : files) {
if(f.isDirectory()){// 判断是否文件夹
resultFileName.add(f.getPath());
ergodic(f,resultFileName);// 调用自身,查找子目录
}else
resultFileName.add(f.getPath());
}
return resultFileName;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
一个文件夹下格式相同excel文件汇总源码
共6个文件
java:1个
jar:1个
classpath:1个
需积分: 50 18 下载量 87 浏览量
2015-02-09
22:58:00
上传
评论
收藏 648KB RAR 举报
温馨提示
本代码通过对文件夹下的excel进行便利,读取excel文件,并将读取的数据写出到新的excel文件中,成功实现在文件具有相同格式时进行文件汇总工作。
资源推荐
资源详情
资源评论
收起资源包目录
totalExcel.rar (6个子文件)
totalExcel
bin
totalExcel
ExportExcel.class 5KB
.settings
org.eclipse.jdt.core.prefs 598B
src
totalExcel
ExportExcel.java 8KB
.project 386B
.classpath 351B
lib
jxl.jar 708KB
共 6 条
- 1
资源评论
贾小牛
- 粉丝: 45
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功