需求如下:
将多个excel文件合并成一个文件,合成后的excel文件只能是一个工作表。使用poi3.7实现。在此请教,会的说一下
注意:仅限于poi3.7 其他的请绕过
将多个excel文件合并成一个文件,合成后的excel文件只能是一个工作表。使用poi3.7实现。在此请教,会的说一下
注意:仅限于poi3.7 其他的请绕过
解决方案 »
- 急,求助。
- 关于BufferedOutputStream写入流
- 批处理时间戳问题求教
- Could not initialize class com.demo.hibernate.util.HibernateSessionFactory
- 各位高手帮帮忙啊,我在MyEclipse里面启动不了Tomcat下面是出错的消息
- HTML内调用saxon.jar
- logic:equal 做下拉框定位的问题
- 初学J2EE,求助入门教程
- 请问各位大侠在设计上如何利用uddi中的分类法
- java中的这个声明赋值访问编译成功了,为什么会出现这种情况呢
- Hibernate延迟加载问题
- httpClient 有关SSL证书的处理。
POI中不是有个Cell类,如果是一个cell整个的copy出来放入新的sheet中,会把样式丢了?
边框是可以设置的。。
楼主,可以参见我的博客,上面关于POI操作Excel的,写的比较详细功能,样式管理等
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class POIDemo { public static void main(String[] args) throws InvalidFormatException, IOException {
Workbook wb = new HSSFWorkbook();
wb.createSheet();
InputStream input = new FileInputStream("filePath1");
tranferValue(input, wb);
input = new FileInputStream("filePath2");
tranferValue(input, wb);
FileOutputStream fileOut = new FileOutputStream("yourExcelName.xls");
try {
wb.write(fileOut);
} catch (Exception e) {
e.printStackTrace();
}finally{
fileOut.close();
} } private static void tranferValue(InputStream input, Workbook outwb) throws InvalidFormatException, IOException{
Sheet outSheet = outwb.getSheetAt(0);
int outLastRowNum = outSheet.getLastRowNum();
if(outLastRowNum>0)outLastRowNum++;
Workbook wb = WorkbookFactory.create(input);
int sheetNums = wb.getNumberOfSheets();
for(int n = 0; n < sheetNums; n++){
Sheet sheet = wb.getSheetAt(n);
int firstRowNum = sheet.getFirstRowNum();
int lastRowNum = sheet.getLastRowNum();
for(int i = firstRowNum; i <= lastRowNum; i++){
Row row = sheet.getRow(i);
if(row != null){
Row outRow = outSheet.createRow(outLastRowNum++);
int firstCellNum = row.getFirstCellNum();
int lastCellNum = row.getLastCellNum();
for(int j = firstCellNum; j < lastCellNum; j++){
Cell cell = row.getCell(j);
Cell outCell = outRow.createCell(j);
if(cell != null){
copyValue(cell, outCell);
copyCellStyle(cell, outCell, outwb);
}
}
}
}
}
}
private static void copyValue(Cell formCell, Cell toCell) {
switch(formCell.getCellType()) {
case Cell.CELL_TYPE_STRING:
toCell.setCellValue(formCell.getRichStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(formCell)) {
toCell.setCellValue(formCell.getDateCellValue());
} else {
toCell.setCellValue(formCell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
toCell.setCellValue(formCell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
toCell.setCellValue(formCell.getCellFormula());
break;
default:
}
}
private static void copyCellStyle(Cell cell, Cell cellout, Workbook wbout) {
CellStyle cellStyleout = wbout.createCellStyle();
cellStyleout.cloneStyleFrom(cell.getCellStyle());
cellout.setCellStyle(cellStyleout);
}
}
这样做出来的有一个bug,就是打开文件会提示部分数据格式丢失
如果是poi生成的excel,应该不会丢失;
但exccel中有些信息恐怕poi也不是100%的支持;