Skip to content

Commit

Permalink
ToHtml
Browse files Browse the repository at this point in the history
  • Loading branch information
getrebuild committed Oct 19, 2023
1 parent 0e28914 commit 8c3e738
Show file tree
Hide file tree
Showing 6 changed files with 730 additions and 5 deletions.
45 changes: 40 additions & 5 deletions src/main/java/com/rebuild/utils/PdfConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

package com.rebuild.utils;

import com.qiniu.util.Json;
import com.rebuild.core.Application;
import com.rebuild.core.service.datareport.ReportsException;
import com.rebuild.core.support.ConfigurationItem;
import com.rebuild.core.support.RebuildConfiguration;
import com.rebuild.core.support.setup.DatabaseBackup;
import com.rebuild.utils.poi.ToHtml;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
Expand All @@ -23,6 +24,8 @@

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;

Expand Down Expand Up @@ -101,9 +104,17 @@ protected static Path convert(Path path, String type, boolean forceRegen) throws
else return dest.toPath();
}

if (TYPE_HTML.equalsIgnoreCase(type) && isWord) {
convertWord2Html(path, dest);
return dest.toPath();
if (TYPE_HTML.equalsIgnoreCase(type)) {
if (isWord) {
convertWord2Html(path, dest);
return dest.toPath();
}
if (isExcel) {
convertExcel2Html(path, dest);
return dest.toPath();
}

throw new ReportsException("CANNOT CONVERT TO HTML : " + pathFileName);
}

// alias
Expand Down Expand Up @@ -141,7 +152,31 @@ protected static void convertWord2Html(Path source, File dest) throws IOExceptio
}

Document html = Jsoup.parse(TEMPALTE_HTML);
html.body().append("<div class=\"paper\">" + cHtml + "</div>");
html.body().append("<div class=\"paper word\">" + cHtml + "</div>");
html.title(source.getFileName().toString());

FileUtils.writeStringToFile(dest, html.html(), AppUtils.UTF8);
}

/**
* Word to HTML
*
* @param source
* @param dest
* @throws IOException
*/
protected static void convertExcel2Html(Path source, File dest) throws IOException {
if (TEMPALTE_HTML == null || Application.devMode()) {
TEMPALTE_HTML = CommonsUtils.getStringOfRes("i18n/html-report.html");
}

StringWriter output = new StringWriter();
ToHtml toHtml = ToHtml.create(Files.newInputStream(source.toFile().toPath()), output);
toHtml.setCompleteHTML(false);
toHtml.printPage();

Document html = Jsoup.parse(TEMPALTE_HTML);
html.body().append("<div class=\"paper excel\">" + output + "</div>");
html.title(source.getFileName().toString());

FileUtils.writeStringToFile(dest, html.html(), AppUtils.UTF8);
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/com/rebuild/utils/poi/HSSFHtmlHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package com.rebuild.utils.poi;

import java.util.Formatter;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.CellStyle;

/**
* Implementation of {@link HtmlHelper} for HSSF files.
*/
public class HSSFHtmlHelper implements HtmlHelper {
private final HSSFWorkbook wb;
private final HSSFPalette colors;

private static final HSSFColor HSSF_AUTO = HSSFColorPredefined.AUTOMATIC.getColor();

public HSSFHtmlHelper(HSSFWorkbook wb) {
this.wb = wb;
// If there is no custom palette, then this creates a new one that is
// a copy of the default
colors = wb.getCustomPalette();
}

@Override
public void colorStyles(CellStyle style, Formatter out) {
HSSFCellStyle cs = (HSSFCellStyle) style;
out.format(" /* fill pattern = %d */%n", cs.getFillPattern().getCode());
styleColor(out, "background-color", cs.getFillForegroundColor());
styleColor(out, "color", cs.getFont(wb).getColor());
styleColor(out, "border-left-color", cs.getLeftBorderColor());
styleColor(out, "border-right-color", cs.getRightBorderColor());
styleColor(out, "border-top-color", cs.getTopBorderColor());
styleColor(out, "border-bottom-color", cs.getBottomBorderColor());
}

private void styleColor(Formatter out, String attr, short index) {
HSSFColor color = colors.getColor(index);
if (index == HSSF_AUTO.getIndex() || color == null) {
out.format(" /* %s: index = %d */%n", attr, index);
} else {
short[] rgb = color.getTriplet();
out.format(" %s: #%02x%02x%02x; /* index = %d */%n", attr, rgb[0], rgb[1], rgb[2], index);
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/rebuild/utils/poi/HtmlHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */

package com.rebuild.utils.poi;

import java.util.Formatter;

import org.apache.poi.ss.usermodel.CellStyle;

/**
* This interface is used where code wants to be independent of the workbook
* formats. If you are writing such code, you can add a method to this
* interface, and then implement it for both HSSF and XSSF workbooks, letting
* the driving code stay independent of format.
*/
public interface HtmlHelper {
/**
* Outputs the appropriate CSS style for the given cell style.
*
* @param style The cell style.
* @param out The place to write the output.
*/
void colorStyles(CellStyle style, Formatter out);
}
Loading

0 comments on commit 8c3e738

Please sign in to comment.