@@ -83,20 +83,29 @@ public class ExcelToHtmlServer { | |||||
} | } | ||||
private void printSheet(Sheet sheet) { | private void printSheet(Sheet sheet) { | ||||
out.format("<table style='word-break:break-all' class=%s>%n", DEFAULTS_CLASS); | |||||
out.format("<table class='%s' width='%s'>%n", DEFAULTS_CLASS, getTableWidth(sheet)); | |||||
printCols(sheet); | printCols(sheet); | ||||
printSheetContent(sheet); | printSheetContent(sheet); | ||||
out.format("</table>%n"); | out.format("</table>%n"); | ||||
} | } | ||||
private void printCols(Sheet sheet) { | private void printCols(Sheet sheet) { | ||||
out.format("<col/>%n"); | |||||
//out.format("<col/>%n"); | |||||
ensureColumnBounds(sheet); | ensureColumnBounds(sheet); | ||||
for (int i = firstColumn; i < endColumn; i++) { | for (int i = firstColumn; i < endColumn; i++) { | ||||
out.format("<col style='width:%spx;' />%n", sheet.getColumnWidth(i) / 32); | out.format("<col style='width:%spx;' />%n", sheet.getColumnWidth(i) / 32); | ||||
} | } | ||||
} | } | ||||
private int getTableWidth(Sheet sheet) { | |||||
ensureColumnBounds(sheet); | |||||
int width = 0; | |||||
for (int i = firstColumn; i < endColumn; i++) { | |||||
width = width + (sheet.getColumnWidth(i) / 32); | |||||
} | |||||
return width; | |||||
} | |||||
private void ensureColumnBounds(Sheet sheet) { | private void ensureColumnBounds(Sheet sheet) { | ||||
if (gotBounds) | if (gotBounds) | ||||
return; | return; | ||||
@@ -134,7 +143,7 @@ public class ExcelToHtmlServer { | |||||
} | } | ||||
private void printSheetContent(Sheet sheet) { | private void printSheetContent(Sheet sheet) { | ||||
printColumnHeads(sheet); | |||||
//printColumnHeads(sheet); | |||||
MergedRegionHelper mergedRegionHelper = new MergedRegionHelper(sheet); | MergedRegionHelper mergedRegionHelper = new MergedRegionHelper(sheet); | ||||
CellValueHelper cellValueHelper = new CellValueHelper(wb, cssRandom); | CellValueHelper cellValueHelper = new CellValueHelper(wb, cssRandom); | ||||
out.format("<tbody>%n"); | out.format("<tbody>%n"); | ||||
@@ -143,7 +152,7 @@ public class ExcelToHtmlServer { | |||||
while (rows.hasNext()) { | while (rows.hasNext()) { | ||||
Row row = rows.next(); | Row row = rows.next(); | ||||
out.format(" <tr style='height:%spx;'>%n", row.getHeight() / 15); | out.format(" <tr style='height:%spx;'>%n", row.getHeight() / 15); | ||||
out.format(" <td class='%s'>%d</td>%n", ROW_HEAD_CLASS, row.getRowNum() + 1); | |||||
//out.format(" <td class='%s'>%d</td>%n", ROW_HEAD_CLASS, row.getRowNum() + 1); | |||||
for (int i = firstColumn; i < endColumn; i++) { | for (int i = firstColumn; i < endColumn; i++) { | ||||
if (mergedRegionHelper.isNeedCreate(rowIndex, i)) { | if (mergedRegionHelper.isNeedCreate(rowIndex, i)) { | ||||
String content = " "; | String content = " "; | ||||
@@ -28,25 +28,34 @@ public class MergedRegionHelper { | |||||
handerMergedString(sheet.getMergedRegion(i).formatAsString()); | handerMergedString(sheet.getMergedRegion(i).formatAsString()); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* 根据合并输出内容,处理合并单元格事情 | * 根据合并输出内容,处理合并单元格事情 | ||||
* @param formatAsString | * @param formatAsString | ||||
*/ | */ | ||||
private void handerMergedString(String formatAsString) { | private void handerMergedString(String formatAsString) { | ||||
String[] strArr = formatAsString.split(":"); | String[] strArr = formatAsString.split(":"); | ||||
int startCol = strArr[0].charAt(0) - 65; | |||||
int startRol = Integer.valueOf(strArr[0].substring(1)); | |||||
int endCol = strArr[1].charAt(0) - 65; | |||||
int endRol = Integer.valueOf(strArr[1].substring(1)); | |||||
mergedCache.put(startRol + "_" + startCol, new Integer[] { endRol - startRol + 1, | |||||
endCol - startCol + 1 }); | |||||
for (int i = startRol; i <= endRol; i++) { | |||||
for (int j = startCol; j <= endCol; j++) { | |||||
notNeedCread.add(i + "_" + j); | |||||
if (strArr.length == 2) { | |||||
int startCol = strArr[0].charAt(0) - 65; | |||||
if (strArr[0].charAt(1) >= 65) { | |||||
startCol = (startCol + 1) * 26 + (strArr[0].charAt(1) - 65); | |||||
} | |||||
int startRol = Integer.valueOf(strArr[0].substring(strArr[0].charAt(1) >= 65 ? 2 : 1)); | |||||
int endCol = strArr[1].charAt(0) - 65; | |||||
if (strArr[1].charAt(1) >= 65) { | |||||
endCol = (endCol + 1) * 26 + (strArr[1].charAt(1) - 65); | |||||
} | |||||
int endRol = Integer.valueOf(strArr[1].substring(strArr[1].charAt(1) >= 65 ? 2 : 1)); | |||||
mergedCache.put(startRol + "_" + startCol, new Integer[] { endRol - startRol + 1, | |||||
endCol - startCol + 1 }); | |||||
for (int i = startRol; i <= endRol; i++) { | |||||
for (int j = startCol; j <= endCol; j++) { | |||||
notNeedCread.add(i + "_" + j); | |||||
} | |||||
} | } | ||||
notNeedCread.remove(startRol + "_" + startCol); | |||||
} | } | ||||
notNeedCread.remove(startRol + "_" + startCol); | |||||
} | } | ||||
/** | /** | ||||
@@ -78,4 +87,5 @@ public class MergedRegionHelper { | |||||
public Integer[] getRowAndColSpan(int row, int col) { | public Integer[] getRowAndColSpan(int row, int col) { | ||||
return mergedCache.get(row + "_" + col); | return mergedCache.get(row + "_" + col); | ||||
} | } | ||||
} | } |
@@ -93,7 +93,7 @@ public class StylerHelper { | |||||
} | } | ||||
private void prontFonts(Workbook wb) { | private void prontFonts(Workbook wb) { | ||||
for (short i = 0, le = wb.getNumberOfFonts(); i < le; i++) { | |||||
for (short i = 0, le = wb.getNumberOfFonts(); i <= le; i++) { | |||||
Font font = wb.getFontAt(i); | Font font = wb.getFontAt(i); | ||||
out.format(".%s .%s {%n", DEFAULTS_CLASS, "font_" + i + "_" + cssRandom); | out.format(".%s .%s {%n", DEFAULTS_CLASS, "font_" + i + "_" + cssRandom); | ||||
fontStyle(font); | fontStyle(font); | ||||
@@ -15,7 +15,7 @@ import org.junit.Test; | |||||
public class ExcelToHtmlUtilTest { | public class ExcelToHtmlUtilTest { | ||||
@Test | |||||
//@Test | |||||
public void testToTableHtmlWorkbook() { | public void testToTableHtmlWorkbook() { | ||||
try { | try { | ||||
Workbook wb = new HSSFWorkbook(new FileInputStream( | Workbook wb = new HSSFWorkbook(new FileInputStream( | ||||
@@ -33,7 +33,7 @@ public class ExcelToHtmlUtilTest { | |||||
} | } | ||||
} | } | ||||
@Test | |||||
//@Test | |||||
public void testToTableHtmlWorkbookInt() { | public void testToTableHtmlWorkbookInt() { | ||||
try { | try { | ||||
Workbook wb = new HSSFWorkbook(new FileInputStream(new File( | Workbook wb = new HSSFWorkbook(new FileInputStream(new File( | ||||
@@ -55,10 +55,12 @@ public class ExcelToHtmlUtilTest { | |||||
try { | try { | ||||
Workbook wb = new HSSFWorkbook(new FileInputStream( | Workbook wb = new HSSFWorkbook(new FileInputStream( | ||||
new File( | |||||
PoiPublicUtil | |||||
.getWebRootPath("org/jeecgframework/poi/test/excel/doc/专项支出用款申请书.xls")))); | |||||
String html = ExcelToHtmlUtil.toAllHtml(wb); | |||||
new File("d:/tt.xls"))); | |||||
// Workbook wb = new HSSFWorkbook(new FileInputStream( | |||||
// new File( | |||||
// PoiPublicUtil | |||||
// .getWebRootPath("org/jeecgframework/poi/test/excel/doc/专项支出用款申请书.xls")))); | |||||
String html = ExcelToHtmlUtil.toTableHtml(wb); | |||||
FileWriter fw = new FileWriter("d:/专项支出用款申请书_all.html"); | FileWriter fw = new FileWriter("d:/专项支出用款申请书_all.html"); | ||||
fw.write(html); | fw.write(html); | ||||
fw.close(); | fw.close(); | ||||
@@ -69,7 +71,7 @@ public class ExcelToHtmlUtilTest { | |||||
} | } | ||||
} | } | ||||
@Test | |||||
//@Test | |||||
public void testToAllHtmlWorkbookInt() { | public void testToAllHtmlWorkbookInt() { | ||||
try { | try { | ||||
Workbook wb = new HSSFWorkbook(new FileInputStream(new File( | Workbook wb = new HSSFWorkbook(new FileInputStream(new File( | ||||