package exceltest;
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
public class excel25 {
public excel25(){
try{
InputStream inp = new FileInputStream("d:/temp/siyodaka.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
int x = sheet.getNumMergedRegions();
//コピー元の4行目から43行目を10回コピーする
for(int i = 0; i < 10; i++){
sheet = sheetCopy(sheet, 3, 43, i, x);
}
FileOutputStream fileOut = new FileOutputStream("d:/temp/workbook.xls");
wb.write(fileOut);
fileOut.close();
}catch(Exception e){
System.out.println(e.toString());
}
}
/*
* sheet worksheet
* st コピーの開始行(start from 0)
* en コピーの最終行(end from 1)
* cnt コピーの位置
* x コピー元のマージの個数
*/
public Sheet sheetCopy(Sheet sheet, int st, int en, int cnt, int x){
Row row = null, row2 = null;
Cell cell = null, cell2 = null;
CellStyle cellstyle = null;
short height = 0;
for(int i = st; i < en; i++){
row = sheet.getRow(i);
height = row.getHeight();
if (row != null){
row2 = sheet.createRow((en - st) * cnt + i);
row2.setHeight(height);
for(int j = 0; j < row.getLastCellNum(); j++){
cell = row.getCell(j);
if(cell != null){
cell2 = row2.createCell(j);
cellstyle = cell.getCellStyle();
cell2.setCellStyle(cellstyle);
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING: cell2.setCellValue(cell.getRichStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)) {
cell2.setCellValue(cell.getDateCellValue());
}else{
cell2.setCellValue(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_FORMULA:
cell2.setCellFormula(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BOOLEAN:
cell2.setCellValue(cell.getBooleanCellValue());
break;
}
}
}
}
}
CellRangeAddress cra = null;
for(int i = 0; i < x; i++){
cra = sheet.getMergedRegion(i);
if (cra.getFirstRow() > st){
cra.setFirstRow(cra.getFirstRow() + (en - st) * cnt);
cra.setLastRow(cra.getLastRow() + (en - st) * cnt);
sheet.addMergedRegion(cra);
}
}
return sheet;
}
public static void main(String[] args) {
// TODO code application logic here
new excel25();
}
}
Attention:
1,从cell1得到的cellStyle,赋给cell2,当cell2修改cellStyle,cell1也会跟着变动。
2,Excel模板继承列属性,不继承行属性,为修改样式而人为添加的单个cell,应添加在模板之外的列,否而修改之后,相应的列也会发生样式变动。
3,当sheet名或者cellvalue是String型时,需先设置编码方式,后再赋值,否则会出现乱码。
hssBook.setSheetName(0, keyList.get(0).getSchoolName(), HSSFWorkbook.ENCODING_UTF_16);
final HSSFCell settingNameCell = row5.createCell(colunmIndex);
settingNameCell.setEncoding(HSSFCell.ENCODING_UTF_16);
settingNameCell.setCellValue(String.format("%s%s", beforeName, l.getName()));
4,自动换行功能
HSSFCellStyle cs=wb.createCellStyle();
cs.setWrapText(true);
cell.setCellStyle(cs);
5,斜线
这个地方是用来在单元格里画斜线的, 原理是在指定的两个点之间画线,然后默认情况此线会随着单元格的变化而变化
HSSFPatriarch patriarch = st.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor();
anchor.setAnchor(cell.getCellNum(), row.getRowNum(), 0, 0, (short) (cell.getCellNum() + 1),row.getRowNum() + 1, 0, 0);
patriarch.createSimpleShape(anchor);
0,0为 x,y坐标
6,合并单元格
hssSheet.addMergedRegion(new Region(rowIndex1,colunmIndex1, rowIndex2, colunmIndex2));