当前位置:文档之家› poi操作Excel中sheet的复制(java版)

poi操作Excel中sheet的复制(java版)

package bos.utils.exportexcel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
} else {
InputStream out = new FileInputStream(toFile);
wbCreat = new HSSFWorkbook(out);
}
File fromFile = new File(fromPath);
if (fromFile.exists()) {
try {
HSSFSheet sheet = wb.getSheetAt(ii);
if (wbCreat.getSheetIndex(sheet.getSheetName()) >= 0) {
continue;
}
HSSFSheet sheetCreat = wbCreat.createSheet(sheet
.getSheetName());
@SuppressWarnings("deprecation")
public static void main(String[] args) throws FileNotFoundException,
IOException {
String fromPath = "C:\\Users\\Administrator\\Desktop\\quinn\\1.xls";// excel存放路径
import ermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import ermodel.Font;
public class CopyExcelSheet{
boolean isHidden=sheet.isColumnHidden(j);
sheetCreat.setColumnHidden(j,isHidden);
}
rowCreat.createCell(j);
//设置高度
rowCreat.getCell(j).getRow().setHeight(
row.getCell(j).getRow().getHeight());
rowCreat.getCell(j).setCellStyle(newStyle);
//复制内容
rowCreat.getCell(j).setCellValue(copyfont(wbCreat, wb, row.getCell(j)));
}
}
}
FileOutputStream fileOut = new FileOutputStream(toFile);
//复制样式
HSSFCellStyle newStyle = wbCreat.createCellStyle();
HSSFCellStyle oldStely = row.getCell(j).getCellStyle();
newStyle=copyStyle(newStyle, wbCreat, oldStely, wb);
wbCreat.write(fileOut);
fileOut.flush();
fileOut.close();
in.close();
} catch (RuntimeException e) {
e.printStackTrace();
}
}
};
/**
* @Title:复制sheet
* @Description:将fromFile文件中的sheet全部复制到toFile中
InputStream in = new FileInputStream(fromFile);
HSSFWorkbook wb = new HSSFWorkbook(in);
for (int ii = 0; ii < wb.getNumberOfSheets(); ii++) {
HSSFSheet sheet = wb.getSheetAt(ii);
import java.io.IOException;
import java.io.InputStream;
import ermodel.HSSFCell;
import ermodel.HSSFCellStyle;
//设置高度
rowCreat.getCell(j).getRow().setHeight(
row.getCell(j).getRow().getHeight());
//复制样式
HSSFCellStyle newStyle = wbCreat.createCellStyle();
HSSFCellStyle oldStely = row.getCell(j).getCellStyle();
}
}
}
FileOutputStream fileOut = new FileOutputStream(toFile);
* @param @param fromPath源文件全路径
* @param @param toPath新文件全路径
* @param @throws IOException
* @return返回类型
* @throws
*/
@SuppressWarnings("deprecation")
public static void copyExcelSheet(String fromPath, String toPath)
import ermodel.HSSFFont;
import ermodel.HSSFRichTextString;
import ermodel.HSSFRow;
import ermodel.HSSFSheeห้องสมุดไป่ตู้;
//取得源有excel Sheet的行
HSSFRow row = sheet.getRow(i);
if (row == null)
continue;
//单元格式样
int firstCell = row.getFirstCellNum();
int lastCell = row.getLastCellNum();
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);
String toPath = "C:\\Users\\Administrator\\Desktop\\quinn\\5.xls";//保存新EXCEL路径
copyExcelSheet(fromPath, toPath);
}
/**
* @Title:根据文件路径复制sheet
* @Description:将fromPath文件中的sheet全部复制到toPath中
int firstRow = sheet.getFirstRowNum();
int lastRow = sheet.getLastRowNum();
for (int i = firstRow; i <= lastRow; i++) {
//创建新建excel Sheet的行
HSSFRow rowCreat = sheetCreat.createRow(i);
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));
* @param @param fromFile .xls源文件
* @param @param toFile .xls新文件
* @param @throws IOException
* @return返回类型
* @throws
*/
@SuppressWarnings("deprecation")
public static void copyExcelSheet(File fromFile, File toFile)
newStyle=copyStyle(newStyle, wbCreat, oldStely, wb);
rowCreat.getCell(j).setCellStyle(newStyle);
//复制内容
rowCreat.getCell(j).setCellValue(copyfont(wbCreat, wb, row.getCell(j)));
sheetCreat.setColumnWidth((short) j,(short) (1.15 * width));
boolean isHidden=sheet.isColumnHidden(j);
sheetCreat.setColumnHidden(j,isHidden);
}
相关主题