poi操作Excel中sheet的复制(java版).doc_第1页
poi操作Excel中sheet的复制(java版).doc_第2页
poi操作Excel中sheet的复制(java版).doc_第3页
poi操作Excel中sheet的复制(java版).doc_第4页
poi操作Excel中sheet的复制(java版).doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

package cn.labsoft.labos.utils.exportexcel;import java.io.File;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.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.Region;import org.apache.poi.ss.usermodel.Font;public class CopyExcelSheetSuppressWarnings(deprecation)public static void main(String args) throws FileNotFoundException,IOException String fromPath = C:UsersAdministratorDesktopquinn1.xls;/ excel存放路径String toPath = C:UsersAdministratorDesktopquinn5.xls;/ 保存新EXCEL路径copyExcelSheet(fromPath, toPath);/* * Title: 根据文件路径复制sheet * Description: 将fromPath文件中的sheet全部复制到toPath中 * param param fromPath 源文件全路径 * param param toPath 新文件全路径 * param throws IOException * return 返回类型 * throws */SuppressWarnings(deprecation)public static void copyExcelSheet(String fromPath, String toPath)throws IOException / 创建新的excelFile toFile = new File(toPath);HSSFWorkbook wbCreat = null;if (!toFile.isFile() wbCreat = new HSSFWorkbook();/建立新HSSFWorkbook对象 else InputStream out = new FileInputStream(toFile);wbCreat = new HSSFWorkbook(out);File fromFile = new File(fromPath);if (fromFile.exists() try InputStream in = new FileInputStream(fromFile);HSSFWorkbook wb = new HSSFWorkbook(in);for (int ii = 0; ii = 0) continue;HSSFSheet sheetCreat = wbCreat.createSheet(sheet.getSheetName();/ 复制源表中的合并单元格MergerRegion(sheetCreat, sheet);int firstRow = sheet.getFirstRowNum();int lastRow = sheet.getLastRowNum();for (int i = firstRow; i = lastRow; i+) / 创建新建excel Sheet的行HSSFRow rowCreat = sheetCreat.createRow(i);/ 取得源有excel Sheet的行HSSFRow row = sheet.getRow(i);if (row = null)continue;/ 单元格式样int firstCell = row.getFirstCellNum();int lastCell = row.getLastCellNum();for (int j = firstCell; j lastCell; j+) if (row.getCell(j) = null)continue;if (i = 0) short width = sheet.getColumnWidth(short) j);sheetCreat.setColumnWidth(short) j,(short) (1.15 * width);boolean isHidden=sheet.isColumnHidden(j);sheetCreat.setColumnHidden(j,isHidden);rowCreat.createCell(j);/ 设置高度rowCreat.getCell(j).getRow().setHeight(row.getCell(j).getRow().getHeight();/复制样式HSSFCellStyle newStyle = wbCreat.createCellStyle();HSSFCellStyle oldStely = row.getCell(j).getCellStyle();newStyle=copyStyle(newStyle, wbCreat, oldStely, wb);rowCreat.getCell(j).setCellStyle(newStyle);/复制内容rowCreat.getCell(j).setCellValue(copyfont(wbCreat, wb, row.getCell(j);FileOutputStream fileOut = new FileOutputStream(toFile);wbCreat.write(fileOut);fileOut.flush();fileOut.close();in.close(); catch (RuntimeException e) e.printStackTrace();/* * Title: 复制sheet * Description: 将fromFile文件中的sheet全部复制到toFile中 * param param fromFile .xls源文件 * param param toFile .xls新文件 * param throws IOException * return 返回类型 * throws */SuppressWarnings(deprecation)public static void copyExcelSheet(File fromFile, File toFile)throws IOException / 创建新的excelHSSFWorkbook wbCreat = null;if (!toFile.isFile() wbCreat = new HSSFWorkbook();/建立新HSSFWorkbook对象 else InputStream out = new FileInputStream(toFile);wbCreat = new HSSFWorkbook(out);if (fromFile.exists() try InputStream in = new FileInputStream(fromFile);HSSFWorkbook wb = new HSSFWorkbook(in);for (int ii = 0; ii = 0) continue;HSSFSheet sheetCreat = wbCreat.createSheet(sheet.getSheetName();/ 复制源表中的合并单元格MergerRegion(sheetCreat, sheet);int firstRow = sheet.getFirstRowNum();int lastRow = sheet.getLastRowNum();for (int i = firstRow; i = lastRow; i+) / 创建新建excel Sheet的行HSSFRow rowCreat = sheetCreat.createRow(i);/ 取得源有excel Sheet的行HSSFRow row = sheet.getRow(i);if (row = null)continue;/ 单元格式样int firstCell = row.getFirstCellNum();int lastCell = row.getLastCellNum();for (int j = firstCell; j =(n+1)HSSFSheet sheet = wb.getSheetAt(n);String sheetName=sheet.getSheetName();int x=1;while(wbCreat.getSheetIndex(sheetName) = 0)sheetName+=(+x+);HSSFSheet sheetCreat = wbCreat.createSheet(sheetName);/ 复制源表中的合并单元格MergerRegion(sheetCreat, sheet);int firstRow = sheet.getFirstRowNum();int lastRow = sheet.getLastRowNum();for (int i = firstRow; i = lastRow; i+) / 创建新建excel Sheet的行HSSFRow rowCreat = sheetCreat.createRow(i);/ 取得源有excel Sheet的行HSSFRow row = sheet.getRow(i);if (row = null)continue;/ 单元格式样int firstCell = row.getFirstCellNum();int lastCell = row.getLastCellNum();for (int j = firstCell; j =0)HSSFSheet sheet = wb.getSheetAt(wb.getSheetIndex(sheetName);int x=1;while(wbCreat.getSheetIndex(sheetName) = 0)sheetName+=(+x+);HSSFSheet sheetCreat = wbCreat.createSheet(sheetName);/ 复制源表中的合并单元格MergerRegion(sheetCreat, sheet);int firstRow = sheet.getFirstRowNum();int lastRow = sheet.getLastRowNum();for (int i = firstRow; i = lastRow; i+) / 创建新建excel Sheet的行HSSFRow rowCreat = sheetCreat.createRow(i);/ 取得源有excel Sheet的行HSSFRow row = sheet.getRow(i);if (row = null)continue;/ 单元格式样int firstCell = row.getFirstCellNum();int lastCell = row.getLastCellNum();for (int j = firstCell; j 0) for (int k = 0; k rts.numFormattingRuns(); k+) toIndex = rts.getIndexOfFormattingRun(k);String temp = rts.toString().substring(fromIndex, toIndex);/判断下标if (fontTemp.getTypeOffset() = HSSFFont.SS_SUB) Font font1 = wbCreat.createFont();font1.setTypeOffset(HSSFFont.SS_SUB);rts.applyFont(fromIndex, toIndex,font1);/判断上标if (fontTemp.getTypeOffset() = HSSFFont.SS_SUPER) Font font2 = wbCreat.createFont();font2.setTypeOffset(HSSFFont.SS_SUPER);rts.applyFont(fromIndex, toIndex,font2);value += temp;if (!value.equals() fontTemp = wb.getFontAt(rts.getFontOfFormattingRun(k);fromIndex = toIndex;toIndex = rts.length();String temp1 = rts.toString().substring(fromIndex, toIndex);if (fontTemp.getTypeOffset() = HSSFFont.SS_SUB) Font font1 = wbCreat.createFont();font1.setTypeOffset(HSSFFont.SS_SUB);rts.applyFont(fromIndex, toIndex,font1);if (fontTemp.getTypeOffset() = HSSFFont.SS_SUPER) Font font2 = wbCreat.createFont();font2.setTypeOffset(HSSFFont.SS_SUPER);rts.applyFont(fromIndex, toIndex,font2);value += temp1;return rts;private static HSSFCellStyle copyStyle(HSSFCellStyle newStyle,HSSFWorkbook wbCreat,HSSFCellStyle oldStely,HSSFWorkbook wb)newStyle.setAlignment(oldStely.getAlignment();newStyle.setBorderTop(oldStely.getBorderTop();newStyle.setBorderBottom(oldStely.getBorderBottom();newStyle.setBorderLeft(oldStely.getBorderLeft();newStyle.setBorderRight(oldStely.getBorderRight();newStyle.setBottomBorderColor(oldStely.getBottomBorderColor();newStyle.setFillBackgroundColor(oldStely.getFillBackgroundColor();newStyle.setDataFormat(oldStely.getDataFormat();newStyle.setFillForegroundColor(oldStely.getFillForegroundColor();newStyle.setFillPattern(oldStely.getFillPattern();newStyle.setHidden(oldStely.getHidden();newStyle.setIndention(oldStely.getIndention();newStyle.setLeftBorderColor(oldStely.getLeftBorderColor();newStyle.setLocked(oldStely.getLocked();newStyle.setRightBorderColor(oldStely.getRightBorderColor();newStyle.setRotation(oldStely.getRotation();newStyle.setTopBorderColor(oldStely.getTopBorderColor();newStyle.setVerticalAlignment(oldStely.getVerticalAlignment();newStyle.setWrapText(oldStely.getWrapText();/设置字体Font newfont = wbCreat.createFont();HSSFFont font = oldStely.getFont(wb);newfont.setFontHeightInPoints(fon

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论