BF [ 1974309 ] Exporting a report to XLS is not setting page format

This commit is contained in:
teo_sarca 2008-06-27 15:26:18 +00:00
parent 6adcc98adb
commit d4d6fea6fb
2 changed files with 105 additions and 28 deletions

View File

@ -67,31 +67,39 @@ public abstract class AbstractExcelExporter
private HSSFFont m_fontHeader = null; private HSSFFont m_fontHeader = null;
private HSSFFont m_fontDefault = null; private HSSFFont m_fontDefault = null;
private Language m_lang = null; private Language m_lang = null;
private int m_sheetCount = 0; private int m_sheetCount = 0;
//
private int m_colSplit = 1;
private int m_rowSplit = 1;
/** Styles cache */ /** Styles cache */
private HashMap<String, HSSFCellStyle> m_styles = new HashMap<String, HSSFCellStyle>(); private HashMap<String, HSSFCellStyle> m_styles = new HashMap<String, HSSFCellStyle>();
public AbstractExcelExporter() { public AbstractExcelExporter() {
m_workbook = new HSSFWorkbook(); m_workbook = new HSSFWorkbook();
m_dataFormat = m_workbook.createDataFormat(); m_dataFormat = m_workbook.createDataFormat();
} }
protected Properties getCtx() { protected Properties getCtx() {
return Env.getCtx(); return Env.getCtx();
} }
protected void setFreezePane(int colSplit, int rowSplit) {
m_colSplit = colSplit;
m_rowSplit = rowSplit;
}
private String fixString(String str) private String fixString(String str)
{ {
// ms excel doesn't support UTF8 charset // ms excel doesn't support UTF8 charset
return Util.stripDiacritics(str); return Util.stripDiacritics(str);
} }
protected Language getLanguage() { protected Language getLanguage() {
if (m_lang == null) if (m_lang == null)
m_lang = Env.getLanguage(getCtx()); m_lang = Env.getLanguage(getCtx());
return m_lang; return m_lang;
} }
private HSSFFont getFont(boolean isHeader) { private HSSFFont getFont(boolean isHeader) {
HSSFFont font = null; HSSFFont font = null;
if (isHeader) { if (isHeader) {
@ -114,7 +122,7 @@ public abstract class AbstractExcelExporter
} }
return font; return font;
} }
/** /**
* Get Excel number format string by given {@link NumberFormat} * Get Excel number format string by given {@link NumberFormat}
* @param df number format * @param df number format
@ -200,7 +208,7 @@ public abstract class AbstractExcelExporter
} }
return cs_header; return cs_header;
} }
private void fixColumnWidth(HSSFSheet sheet, int lastColumnIndex) private void fixColumnWidth(HSSFSheet sheet, int lastColumnIndex)
{ {
/* POI 3.0.1 * /* POI 3.0.1 *
@ -209,14 +217,15 @@ public abstract class AbstractExcelExporter
} }
/**/ /**/
} }
private void closeTableSheet(HSSFSheet prevSheet, String prevSheetName, int colCount) private void closeTableSheet(HSSFSheet prevSheet, String prevSheetName, int colCount)
{ {
if (prevSheet == null) if (prevSheet == null)
return; return;
// //
fixColumnWidth(prevSheet, colCount); fixColumnWidth(prevSheet, colCount);
prevSheet.createFreezePane(1, 1); if (m_colSplit >= 0 || m_rowSplit >= 0)
prevSheet.createFreezePane(m_colSplit >= 0 ? m_colSplit : 0, m_rowSplit >= 0 ? m_rowSplit : 0);
if (!Util.isEmpty(prevSheetName, true) && m_sheetCount > 0) { if (!Util.isEmpty(prevSheetName, true) && m_sheetCount > 0) {
int prevSheetIndex = m_sheetCount - 1; int prevSheetIndex = m_sheetCount - 1;
try { try {
@ -230,28 +239,14 @@ public abstract class AbstractExcelExporter
private HSSFSheet createTableSheet() private HSSFSheet createTableSheet()
{ {
HSSFSheet sheet= m_workbook.createSheet(); HSSFSheet sheet= m_workbook.createSheet();
sheet.setFitToPage(true); formatPage(sheet);
// Print Setup createHeaderFooter(sheet);
HSSFPrintSetup ps = sheet.getPrintSetup();
sheet.setAutobreaks(true);
ps.setFitWidth((short)1);
ps.setNoColor(true);
// Sheet Header
HSSFHeader header = sheet.getHeader();
header.setRight(HSSFHeader.page()+ " / "+HSSFHeader.numPages());
// Sheet Footer
HSSFFooter footer = sheet.getFooter();
footer.setLeft(Adempiere.ADEMPIERE_R);
footer.setCenter(Env.getHeader(getCtx(), 0));
Timestamp now = new Timestamp(System.currentTimeMillis());
footer.setRight(DisplayType.getDateFormat(DisplayType.DateTime, getLanguage()).format(now));
// Table Header
createTableHeader(sheet); createTableHeader(sheet);
m_sheetCount++; m_sheetCount++;
// //
return sheet; return sheet;
} }
private void createTableHeader(HSSFSheet sheet) private void createTableHeader(HSSFSheet sheet)
{ {
short colnumMax = 0; short colnumMax = 0;
@ -282,6 +277,30 @@ public abstract class AbstractExcelExporter
// m_workbook.setRepeatingRowsAndColumns(m_sheetCount, 0, 0, 0, 0); // m_workbook.setRepeatingRowsAndColumns(m_sheetCount, 0, 0, 0, 0);
} }
protected void createHeaderFooter(HSSFSheet sheet)
{
// Sheet Header
HSSFHeader header = sheet.getHeader();
header.setRight(HSSFHeader.page()+ " / "+HSSFHeader.numPages());
// Sheet Footer
HSSFFooter footer = sheet.getFooter();
footer.setLeft(Adempiere.ADEMPIERE_R);
footer.setCenter(Env.getHeader(getCtx(), 0));
Timestamp now = new Timestamp(System.currentTimeMillis());
footer.setRight(DisplayType.getDateFormat(DisplayType.DateTime, getLanguage()).format(now));
}
protected void formatPage(HSSFSheet sheet)
{
sheet.setFitToPage(true);
// Print Setup
HSSFPrintSetup ps = sheet.getPrintSetup();
ps.setFitWidth((short)1);
ps.setNoColor(true);
ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
ps.setLandscape(false);
}
/** /**
* Export to given stream * Export to given stream
* @param out * @param out
@ -386,7 +405,7 @@ public abstract class AbstractExcelExporter
{ {
export(file, language, true); export(file, language, true);
} }
/** /**
* Export to file * Export to file
* @param file * @param file

View File

@ -16,9 +16,14 @@ package org.adempiere.print.export;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Date; import java.util.Date;
import javax.print.attribute.standard.MediaSizeName;
import org.adempiere.impexp.AbstractExcelExporter; import org.adempiere.impexp.AbstractExcelExporter;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.compiere.print.MPrintFormat; import org.compiere.print.MPrintFormat;
import org.compiere.print.MPrintFormatItem; import org.compiere.print.MPrintFormatItem;
import org.compiere.print.MPrintPaper;
import org.compiere.print.PrintData; import org.compiere.print.PrintData;
import org.compiere.print.PrintDataElement; import org.compiere.print.PrintDataElement;
@ -26,6 +31,7 @@ import org.compiere.print.PrintDataElement;
* Export PrintData to Excel (XLS) file * Export PrintData to Excel (XLS) file
* @author Teo Sarca, SC ARHIPAC SERVICE SRL * @author Teo Sarca, SC ARHIPAC SERVICE SRL
* <li>BF [ 1939010 ] Excel Export ERROR - java.sql.Date - integrated Mario Grigioni's fix * <li>BF [ 1939010 ] Excel Export ERROR - java.sql.Date - integrated Mario Grigioni's fix
* <li>BF [ 1974309 ] Exporting a report to XLS is not setting page format
*/ */
public class PrintDataExcelExporter public class PrintDataExcelExporter
extends AbstractExcelExporter extends AbstractExcelExporter
@ -131,4 +137,56 @@ extends AbstractExcelExporter
protected boolean isFunctionRow() { protected boolean isFunctionRow() {
return m_printData.isFunctionRow(); return m_printData.isFunctionRow();
} }
@Override
protected void formatPage(HSSFSheet sheet) {
super.formatPage(sheet);
MPrintPaper paper = MPrintPaper.get(this.m_printFormat.getAD_PrintPaper_ID());
//
// Set paper size:
short paperSize = -1;
MediaSizeName mediaSizeName = paper.getMediaSize().getMediaSizeName();
if (MediaSizeName.NA_LETTER.equals(mediaSizeName)) {
paperSize = HSSFPrintSetup.LETTER_PAPERSIZE;
}
else if (MediaSizeName.NA_LEGAL.equals(mediaSizeName)) {
paperSize = HSSFPrintSetup.LEGAL_PAPERSIZE;
}
else if (MediaSizeName.EXECUTIVE.equals(mediaSizeName)) {
paperSize = HSSFPrintSetup.EXECUTIVE_PAPERSIZE;
}
else if (MediaSizeName.ISO_A4.equals(mediaSizeName)) {
paperSize = HSSFPrintSetup.A4_PAPERSIZE;
}
else if (MediaSizeName.ISO_A5.equals(mediaSizeName)) {
paperSize = HSSFPrintSetup.A5_PAPERSIZE;
}
else if (MediaSizeName.NA_NUMBER_10_ENVELOPE.equals(mediaSizeName)) {
paperSize = HSSFPrintSetup.ENVELOPE_10_PAPERSIZE;
}
// else if (MediaSizeName..equals(mediaSizeName)) {
// paperSize = HSSFPrintSetup.ENVELOPE_DL_PAPERSIZE;
// }
// else if (MediaSizeName..equals(mediaSizeName)) {
// paperSize = HSSFPrintSetup.ENVELOPE_CS_PAPERSIZE;
// }
else if (MediaSizeName.MONARCH_ENVELOPE.equals(mediaSizeName)) {
paperSize = HSSFPrintSetup.ENVELOPE_MONARCH_PAPERSIZE;
}
if (paperSize != -1) {
sheet.getPrintSetup().setPaperSize(paperSize);
}
//
// Set Landscape/Portrait:
sheet.getPrintSetup().setLandscape(paper.isLandscape());
//
// Set Paper Margin:
sheet.setMargin(HSSFSheet.TopMargin, ((double)paper.getMarginTop()) /72);
sheet.setMargin(HSSFSheet.RightMargin, ((double)paper.getMarginRight()) / 72);
sheet.setMargin(HSSFSheet.LeftMargin, ((double)paper.getMarginLeft()) / 72);
sheet.setMargin(HSSFSheet.BottomMargin, ((double)paper.getMarginBottom()) / 72);
//
}
} }