forked from fanruan/demo-export-xml
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
4.3 KiB
117 lines
4.3 KiB
package com.fr.plugin.export.xml.exporter; |
|
|
|
import com.fr.base.Style; |
|
import com.fr.general.FRLogger; |
|
import com.fr.general.GeneralUtils; |
|
import com.fr.io.core.ExporterUtils; |
|
import com.fr.io.exporter.AbstractAppExporter; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.main.workbook.ResultWorkBook; |
|
import com.fr.page.ClippedPageProvider; |
|
import com.fr.page.PageSetProvider; |
|
import com.fr.page.PaperSettingProvider; |
|
import com.fr.page.ReportPageProvider; |
|
import com.fr.plugin.PluginLicense; |
|
import com.fr.plugin.PluginLicenseManager; |
|
import com.fr.plugin.export.xml.core.XmlConstants; |
|
import com.fr.report.cell.CellElement; |
|
import com.fr.report.cell.cellattr.CellGUIAttr; |
|
import com.fr.report.core.ReportUtils; |
|
import com.fr.report.elementcase.ElementGetter; |
|
import com.fr.report.report.ECReport; |
|
import com.fr.report.report.ResultReport; |
|
import com.fr.stable.EncodeConstants; |
|
import com.fr.stable.StringUtils; |
|
|
|
import java.io.OutputStream; |
|
import java.io.OutputStreamWriter; |
|
import java.io.PrintWriter; |
|
import java.text.Format; |
|
import java.util.Iterator; |
|
import java.util.List; |
|
|
|
/** |
|
* Created by richie on 16/1/21. |
|
*/ |
|
public class XmlExporter extends AbstractAppExporter { |
|
|
|
@Override |
|
public void export(OutputStream out, ResultWorkBook book) throws Exception { |
|
List paperSettingList = ReportUtils.getPaperSettingListFromWorkBook(book); |
|
for (int i = 0, len = book.getReportCount(); i < len; i++) { |
|
this.export(out, book.getResultReport(i), (PaperSettingProvider) paperSettingList.get(i)); |
|
} |
|
} |
|
|
|
@Override |
|
public void export(java.io.OutputStream out, PageSetProvider pageSet) throws Exception { |
|
for (int i = 0; i < pageSet.size(); i++) { |
|
ReportPageProvider reportPage = pageSet.getPage(i); |
|
ClippedPageProvider page = ExporterUtils.support(reportPage); |
|
if (page == null) { |
|
break; |
|
} |
|
this.exportReport(out, (ElementGetter) page, 0, (page).getRowCount()); |
|
} |
|
} |
|
|
|
private void export(OutputStream out, ResultReport report, PaperSettingProvider paperSetting) |
|
throws Exception { |
|
if (report != null) { |
|
FineLoggerFactory.getLogger().info("UnLayerReport start export"); |
|
exportReport(out, (ECReport) report, 0, (report).getRowCount()); |
|
} |
|
} |
|
|
|
public void exportReport(OutputStream out, ElementGetter reportCase, int start, int end) throws Exception { |
|
PluginLicense license = PluginLicenseManager.getInstance().getPluginLicenseByID(XmlConstants.PLUGIN_ID); |
|
if (license != null && license.isAvailable()) { |
|
PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, EncodeConstants.ENCODING_UTF_8)); |
|
StringBuilder xmlBuffer = new StringBuilder(); |
|
xmlBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"); |
|
xmlBuffer.append("<report>"); |
|
for (int row = start; row < end; row++) { |
|
xmlBuffer.append("<row>"); |
|
Iterator it = reportCase.getRow(row); |
|
|
|
while (it.hasNext()) { |
|
xmlBuffer.append("<col>"); |
|
CellElement cell = (CellElement) it.next(); |
|
Object value = getCellValue(cell); |
|
Style style = cell.getStyle(); |
|
String str; |
|
if (style != null) { |
|
Format format = style.getFormat(); |
|
str = Style.valueToText(value, format); |
|
} else { |
|
str = GeneralUtils.objectToString(value); |
|
} |
|
xmlBuffer.append(str); |
|
xmlBuffer.append("</col>"); |
|
|
|
} |
|
xmlBuffer.append("</row>"); |
|
} |
|
xmlBuffer.append("</report>"); |
|
writer.println(xmlBuffer.toString()); |
|
writer.flush(); |
|
} else { |
|
throw new RuntimeException("XML Export Plugin License Expired!"); |
|
} |
|
} |
|
|
|
|
|
private Object getCellValue(CellElement cell) { |
|
if (cell == null) { |
|
return null; |
|
} |
|
Object value = cell.getValue(); |
|
CellGUIAttr gui = cell.getCellGUIAttr(); |
|
if (gui != null && !gui.isPrintContent()) { |
|
value = StringUtils.EMPTY; |
|
} |
|
return value == null ? StringUtils.EMPTY : value; |
|
} |
|
|
|
|
|
} |