各位兄弟姐妹,
有谁做过用poi(资源包)合并excel文件吗?比如:
有 1.xls, 2.xls 2个excel文件。
我现在想把这2个excel文件合并成一个新的excel文件。 之前2个excel里面的内容都不变。相当于复制他们的sheet。有谁弄过嘛?急啊
小弟感谢不尽!
有谁做过用poi(资源包)合并excel文件吗?比如:
有 1.xls, 2.xls 2个excel文件。
我现在想把这2个excel文件合并成一个新的excel文件。 之前2个excel里面的内容都不变。相当于复制他们的sheet。有谁弄过嘛?急啊
小弟感谢不尽!
还有一点说明下。 生成的新的excel文件的sheet个数等于1跟2sheet的之和。他们的具体内容不变!
相当于新生产的excel文件 有2个sheet, 分别是1和2的sheet
就是要把2个excel文件合并起来!!
1个excel文件只有1个sheet。1跟2 合并起来。 生成的新的一定是2个sheet,内容不变!
需求就是这样的!
http://kb.cnblogs.com/b/160588/
来设置。应该够你用了
这个代码 我看过 不符合我的要求。他是在同一个excel文件里,进行复制的
我这个是2个不同的excel文件啊大侠,请指教
一直在等中。
CSDN的大侠 救命啊。等待中
HSSFSheet tmpSheet = workbook.getSheet( "Sheet Name" );
HSSFWorkbook workbook2 = new HSSFWorkbook();
workbook2.addSheet( tmpSheet );因为HSSFWorkbook 里并没有实现addSheet这个函数
http://marc.info/?l=poi-dev&m=102185037406467&w=2
> > {
> >
> > // if (getNumberOfSheets() == 3)
> > // throw new RuntimeException("You cannot have more than three
> > sheets in HSSF 1.0");
> > HSSFSheet locSheet = new HSSFSheet(workbook);
> >
> > short j=0;
> > file://locSheet.setDefaultColumnWidth(mySheet.getColumnWidth(j));
> >
> >
> > // HSSFCell locCellObj=locRowObj.createCell(0,1);
> > int noRow = mySheet.getPhysicalNumberOfRows();
> > System.out.println("Row "+noRow);
> > int cellType = 0;
> > for(int i=0; i<noRow; i++){
> >
> > HSSFRow locRowObj=locSheet.createRow((short)i);
> > HSSFRow myRowObj=mySheet.getRow(i);
> > int noCells = myRowObj.getPhysicalNumberOfCells();
> > for(int k=0; k<noCells; k++){
> >
> > HSSFCell myCellObj=myRowObj.getPhysicalCellAt(k);
> > file://myCellObj.
> >
> > file://HSSFCell
> > locCellObj=locRowObj.createCell((short)myCellObj.getNumericCellValue()
> > ,myCellObj.getCellType());
> >
> >
> > cellType = myCellObj.getCellType();
> > HSSFCell
> > locCellObj=locRowObj.createCell(myCellObj.getCellNum(),cellType);
> > locCellObj.setCellStyle((myCellObj.getCellStyle()));
> > System.out.println(myCellObj.getCellType());
> >
> >
> > if(cellType==0)
> > {
> > System.out.println("Number:"+cellType);
> > locCellObj.setCellStyle(myCellObj.getCellStyle());
> > locCellObj.setCellValue(myCellObj.getNumericCellValue());
> > System.out.println(myCellObj.getNumericCellValue());
> > }
> > else if(cellType==3)
> > {
> > file://Nothing. Blank cell already created.
> > }
> >
> > else if(cellType==4)
> > {
> > file://Formula.
> >
> > }
> >
> > else
> > {
> > System.out.println("String:"+cellType);
> > locCellObj.setCellStyle(myCellObj.getCellStyle());
> > locCellObj.setCellValue(myCellObj.getStringCellValue());
> > System.out.println(myCellObj.getStringCellValue());
> > }
> >
> >
> > System.out.println("Transco");
> > }
> > file://locRowObj.=myRowObj.;
> > }
> >
> > sheets.add(locSheet);
> > workbook.setSheetName(sheets.size() - 1,
> > "Sheet" + (sheets.size() - 1));
> > file://return sheet;
> > }
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;public class AA { static HSSFWorkbook wbTotal = null;
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
AA a = new AA();
a.test();
}
public void test() throws IOException{
String path="d:/test.xls";
HSSFWorkbook one = this.OrgTrade();
HSSFWorkbook two = this.CompanyCount();
FileOutputStream fileOut = new FileOutputStream(path);
one.write(fileOut);
two.write(fileOut);
fileOut.close();
}
public HSSFWorkbook OrgTrade() throws IOException{
HSSFWorkbook one =null;
String filePath = "c://" + "reportResponse" + "/" + "1" + "/"+ "report-day";
File root = new File(filePath);
String time = "2009-12-3"; // 日期
String a[] = root.list();
if (a != null && a.length > 0) {
for (int i = 0; i < a.length; i++) {
String fileName = a[i];
String newFileName = fileName.substring(0, fileName.lastIndexOf("."));
if (time.equals(newFileName)) {
String fullFilePathName = filePath + "/" + fileName;
FileInputStream is = new FileInputStream(fullFilePathName);
one = new HSSFWorkbook(is);
//wbTotal.write(new FileOutputStream(one.g));
//one.createSheet("one");
//wbTotal.createSheet(one.getSheetAt(0).);
}
}
}
return one;
}
//
public HSSFWorkbook CompanyCount() throws IOException{
HSSFWorkbook two =null;
String filePath1 = "c://" + "reportResponse" + "/" + "2" + "/"+ "report-day";
File root1 = new File(filePath1);
String time1 = "2009-12-1"; // 日期
String a1[] = root1.list();
if (a1 != null && a1.length > 0) {
for (int i = 0; i < a1.length; i++) {
String fileName = a1[i];
String newFileName = fileName.substring(0, fileName.lastIndexOf("."));
if (time1.equals(newFileName)) {
String fullFilePathName = filePath1 + "/" + fileName;
FileInputStream is = new FileInputStream(fullFilePathName);
two = new HSSFWorkbook(is);
}
}
}
return two;
}}
大家看我写的代码
我的意思很明确,就是想把1.xls, 2.xls里的2个sheet 生成到一个新的excel文件中。请大家直接啊
就是还有2的 没生成。
请大家帮我看看啊
急中
红色部分不对吧,我的理解是HSSFWorkbook就是一个excel模型,然后它里面包含n个HSSFSheet,你写两个excel模型到文件应该不行的吧
如果一个一个单元格复制 只要能实现 也可以啊
我这里没啥font、cellstyle之类的定义
求救中!