ASF Bugzilla – Attachment 32616 Details for
Bug 57768
CellStyle.cloneStyleFrom(...) does not copying background colors and borders
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Java code
TestCopyFileExcel.java (text/plain), 4.59 KB, created by
Michael
on 2015-03-27 15:48:48 UTC
(
hide
)
Description:
Java code
Filename:
MIME Type:
Creator:
Michael
Created:
2015-03-27 15:48:48 UTC
Size:
4.59 KB
patch
obsolete
>import java.io.*; >import java.util.*; > >import org.apache.poi.xssf.usermodel.*; >import org.apache.poi.ss.usermodel.*; >import org.apache.poi.ss.util.*; >import org.apache.poi.openxml4j.opc.*; > >public class TestCopyFileExcel >{ > public static void main(String[] args) > { > TestCopyFileExcel test = new TestCopyFileExcel(); > try{ > test.copy("TestTemplate.xlsx", "TestReport.xlsx"); > } catch(Exception e) { > e.printStackTrace(); > } > } > > public void copy(String templateName, String reportName) throws Exception > { > File templateFile = new File(templateName); > if (!templateFile.exists()) { > throw new FileNotFoundException("The template '" + templateName + "' not found!"); > } > > XSSFWorkbook template = (XSSFWorkbook)WorkbookFactory.create(templateFile); > > File reportFile = new File(reportName); > if(reportFile.exists() && !reportFile.delete()){ > throw new Exception("Failed to delete existing file " + reportName); > } > > if(!reportFile.createNewFile()){ > throw new Exception("Failed to create new file " + reportName); > } > > XSSFWorkbook report = new XSSFWorkbook(); > for(int i = 0; i < template.getNumberOfSheets(); i++) > { > XSSFSheet sourceSheet = template.getSheetAt(i); > XSSFSheet destSheet = report.createSheet(sourceSheet.getSheetName()); > > copySheet(sourceSheet, destSheet, report); > > int numMergedRegions = sourceSheet.getNumMergedRegions(); > for(int j = 0; j < numMergedRegions; j++) > { > CellRangeAddress range = sourceSheet.getMergedRegion(j); > destSheet.addMergedRegion(new CellRangeAddress(range.getFirstRow(), range.getLastRow(), > range.getFirstColumn(), range.getLastColumn())); > } > } > > template.close(); > > FileOutputStream fos = new FileOutputStream(reportFile); > report.write(fos); > fos.close(); > > System.out.println("Success!"); > } > > public void copySheet(XSSFSheet sourceSheet, XSSFSheet destSheet, XSSFWorkbook destWb) throws Exception > { > for(Row sourceRow : sourceSheet) > { > XSSFRow destRow = destSheet.createRow(sourceRow.getRowNum()); > copyRow((XSSFRow)sourceRow, destRow, destWb); > } > } > > public void copyRow(XSSFRow sourceRow, XSSFRow destRow, XSSFWorkbook destWb) throws Exception > { > destRow.setHeight(sourceRow.getHeight()); > XSSFCellStyle sourceStyle = (XSSFCellStyle)sourceRow.getRowStyle(); > XSSFCellStyle destStyle = (XSSFCellStyle)destWb.createCellStyle(); > copyCellStyle(sourceStyle, destStyle, destWb); > destRow.setRowStyle(destStyle); > for(Cell sourceCell : sourceRow) > { > XSSFCell destCell = destRow.createCell(sourceCell.getColumnIndex()); > copyCell((XSSFCell)sourceCell, destCell, destWb); > } > } > > public void copyCell(XSSFCell sourceCell, XSSFCell destCell, XSSFWorkbook destWb) throws Exception > { > int type = sourceCell.getCellType(); > destCell.setCellType(type); > switch (type) > { > case Cell.CELL_TYPE_FORMULA: > destCell.setCellFormula(sourceCell.getCellFormula()); > break; > case Cell.CELL_TYPE_BOOLEAN: > destCell.setCellValue(sourceCell.getBooleanCellValue()); > break; > case Cell.CELL_TYPE_NUMERIC: > destCell.setCellValue(sourceCell.getNumericCellValue()); > break; > case Cell.CELL_TYPE_ERROR: > destCell.setCellErrorValue(sourceCell.getErrorCellValue()); > break; > case Cell.CELL_TYPE_STRING: > destCell.setCellValue(sourceCell.getRichStringCellValue()); > break; > case Cell.CELL_TYPE_BLANK: > default: > break; > } > > destCell.setCellComment(sourceCell.getCellComment()); > > XSSFCellStyle sourceStyle = sourceCell.getCellStyle(); > XSSFCellStyle destStyle = destWb.createCellStyle(); > copyCellStyle(sourceStyle, destStyle, destWb); > > destCell.setCellStyle(destStyle); > > destCell.getSheet().setColumnWidth(sourceCell.getColumnIndex(), sourceCell.getSheet().getColumnWidth(sourceCell.getColumnIndex())); > } > > public void copyCellStyle(XSSFCellStyle sourceStyle, XSSFCellStyle destStyle, XSSFWorkbook destWb) throws Exception > { > if(sourceStyle == null){ > return; > } > > destStyle.cloneStyleFrom(sourceStyle); > > //Copy borders > destStyle.setBorderBottom(sourceStyle.getBorderBottomEnum()); > destStyle.setBottomBorderColor(sourceStyle.getBottomBorderXSSFColor()); > > destStyle.setBorderLeft(sourceStyle.getBorderLeftEnum()); > destStyle.setLeftBorderColor(sourceStyle.getLeftBorderXSSFColor()); > > destStyle.setBorderRight(sourceStyle.getBorderRightEnum()); > destStyle.setRightBorderColor(sourceStyle.getRightBorderXSSFColor()); > > destStyle.setBorderTop(sourceStyle.getBorderTopEnum()); > destStyle.setTopBorderColor(sourceStyle.getTopBorderXSSFColor()); > > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 57768
:
32615
| 32616