Преглед на файлове

修复删除带来的宽度变现问题

4.1.3.A
jueyue преди 7 години
родител
ревизия
a6746fac93
променени са 2 файла, в които са добавени 128 реда и са изтрити 127 реда
  1. +18
    -17
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java
  2. +110
    -110
      easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiSheetUtil.java

+ 18
- 17
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java Целия файл

@@ -1,6 +1,6 @@
/**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
*
* Licensed 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
@@ -55,11 +55,11 @@ import cn.afterturn.easypoi.exception.excel.ExcelExportException;
import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum;
import cn.afterturn.easypoi.util.PoiExcelGraphDataUtil;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import cn.afterturn.easypoi.util.PoiSheetUtility;
import cn.afterturn.easypoi.util.PoiSheetUtil;
/**
* Excel 导出根据模板导出
*
*
* @author JueYue
* 2013-10-17
* @version 1.0
@@ -86,7 +86,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService {
/**
* 往Sheet 填充正常数据,根据表头信息 使用导入的部分逻辑,坐对象映射
*
*
* @param sheet
* @param pojoClass
* @param dataSet
@@ -147,7 +147,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService {
/**
* 获取单个对象的高度,主要是处理一堆多的情况
*
*
* @param t
* @param excelParams
* @throws Exception
@@ -212,7 +212,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService {
/**
* 克隆excel防止操作原对象,workbook无法克隆,只能对excel进行克隆
*
*
* @throws Exception
*/
private Workbook getCloneWorkBook() throws Exception {
@@ -223,7 +223,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService {
/**
* 获取表头数据,设置表头的序号
*
*
* @param sheet
* @return
*/
@@ -352,7 +352,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService {
* 先判断删除,省得影响效率
* @param sheet
* @param map
* @throws Exception
* @throws Exception
*/
private void deleteCell(Sheet sheet, Map<String, Object> map) throws Exception {
Row row = null;
@@ -373,7 +373,8 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService {
if (Boolean.valueOf(
eval(text.substring(text.indexOf(START_STR) + 2, text.indexOf(END_STR))
.trim(), map).toString())) {
PoiSheetUtility.deleteColumn(sheet, i);
PoiSheetUtil.deleteColumn(sheet, i);
i--;
}
cell.setCellValue("");
}
@@ -384,7 +385,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService {
/**
* 给每个Cell通过解析方式set值
*
*
* @param cell
* @param map
*/
@@ -432,10 +433,10 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService {
/**
* 利用foreach循环输出数据
* @param cell
* @param cell
* @param map
* @param name
* @throws Exception
* @throws Exception
*/
private void addListDataToExcel(Cell cell, Map<String, Object> map,
String name) throws Exception {
@@ -484,8 +485,8 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService {
/**
* 创建并返回第一个Row
* @param sheet
* @param rowIndex
* @param sheet
* @param rowIndex
* @param isCreate
* @param rows
* @return
@@ -617,7 +618,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService {
* 获取迭代的数据的值
* @param cell
* @param name
* @param mergedRegionHelper
* @param mergedRegionHelper
* @return
*/
private Object[] getAllDataColumns(Cell cell, String name,
@@ -693,7 +694,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService {
* 获取模板参数
* @param name
* @param cell
* @param mergedRegionHelper
* @param mergedRegionHelper
* @return
*/
private ExcelForEachParams getExcelTemplateParams(String name, Cell cell,
@@ -724,7 +725,7 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService {
/**
* 对导出序列进行排序和塞选
*
*
* @param excelParams
* @param titlemap
*/


easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiSheetUtility.java → easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiSheetUtil.java Целия файл

@@ -1,111 +1,111 @@
/**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
* Licensed 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 cn.afterturn.easypoi.util;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
/**
* 国外高手的,不过也不好,慎用,效率不行
* Helper functions to aid in the management of sheets
*/
public class PoiSheetUtility extends Object {
/**
* Given a sheet, this method deletes a column from a sheet and moves
* all the columns to the right of it to the left one cell.
*
* Note, this method will not update any formula references.
*
* @param sheet
* @param columnToDelete
*/
public static void deleteColumn(Sheet sheet, int columnToDelete) {
int maxColumn = 0;
for (int r = 0; r < sheet.getLastRowNum() + 1; r++) {
Row row = sheet.getRow(r);
// if no row exists here; then nothing to do; next!
if (row == null) {
continue;
}
// if the row doesn't have this many columns then we are good; next!
int lastColumn = row.getLastCellNum();
if (lastColumn > maxColumn) {
maxColumn = lastColumn;
}
if (lastColumn < columnToDelete) {
continue;
}
for (int x = columnToDelete + 1; x < lastColumn + 1; x++) {
Cell oldCell = row.getCell(x - 1);
if (oldCell != null) {
row.removeCell(oldCell);
}
Cell nextCell = row.getCell(x);
if (nextCell != null) {
Cell newCell = row.createCell(x - 1, nextCell.getCellType());
cloneCell(newCell, nextCell);
}
}
}
// Adjust the column widths
for (int c = 0; c < maxColumn; c++) {
sheet.setColumnWidth(c, sheet.getColumnWidth(c + 1));
}
}
/*
* Takes an existing Cell and merges all the styles and forumla
* into the new one
*/
private static void cloneCell(Cell cNew, Cell cOld) {
cNew.setCellComment(cOld.getCellComment());
cNew.setCellStyle(cOld.getCellStyle());
switch (cNew.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN: {
cNew.setCellValue(cOld.getBooleanCellValue());
break;
}
case Cell.CELL_TYPE_NUMERIC: {
cNew.setCellValue(cOld.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_STRING: {
cNew.setCellValue(cOld.getStringCellValue());
break;
}
case Cell.CELL_TYPE_ERROR: {
cNew.setCellValue(cOld.getErrorCellValue());
break;
}
case Cell.CELL_TYPE_FORMULA: {
cNew.setCellFormula(cOld.getCellFormula());
break;
}
default:
cNew.setCellValue(cOld.getStringCellValue());
}
}
/**
* Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
*
* Licensed 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 cn.afterturn.easypoi.util;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
/**
* 国外高手的,不过也不好,慎用,效率不行
* Helper functions to aid in the management of sheets
*/
public class PoiSheetUtil {
/**
* Given a sheet, this method deletes a column from a sheet and moves
* all the columns to the right of it to the left one cell.
*
* Note, this method will not update any formula references.
*
* @param sheet
* @param columnToDelete
*/
public static void deleteColumn(Sheet sheet, int columnToDelete) {
int maxColumn = 0;
for (int r = 0; r < sheet.getLastRowNum() + 1; r++) {
Row row = sheet.getRow(r);
// if no row exists here; then nothing to do; next!
if (row == null) {
continue;
}
// if the row doesn't have this many columns then we are good; next!
int lastColumn = row.getLastCellNum();
if (lastColumn > maxColumn) {
maxColumn = lastColumn;
}
if (lastColumn < columnToDelete) {
continue;
}
for (int x = columnToDelete + 1; x < lastColumn + 1; x++) {
Cell oldCell = row.getCell(x - 1);
if (oldCell != null) {
row.removeCell(oldCell);
}
Cell nextCell = row.getCell(x);
if (nextCell != null) {
Cell newCell = row.createCell(x - 1, nextCell.getCellType());
cloneCell(newCell, nextCell);
}
}
}
// Adjust the column widths
for (int c = columnToDelete; c < maxColumn; c++) {
sheet.setColumnWidth(c, sheet.getColumnWidth(c + 1));
}
}
/*
* Takes an existing Cell and merges all the styles and forumla
* into the new one
*/
private static void cloneCell(Cell cNew, Cell cOld) {
cNew.setCellComment(cOld.getCellComment());
cNew.setCellStyle(cOld.getCellStyle());
switch (cNew.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN: {
cNew.setCellValue(cOld.getBooleanCellValue());
break;
}
case Cell.CELL_TYPE_NUMERIC: {
cNew.setCellValue(cOld.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_STRING: {
cNew.setCellValue(cOld.getStringCellValue());
break;
}
case Cell.CELL_TYPE_ERROR: {
cNew.setCellValue(cOld.getErrorCellValue());
break;
}
case Cell.CELL_TYPE_FORMULA: {
cNew.setCellFormula(cOld.getCellFormula());
break;
}
default:
cNew.setCellValue(cOld.getStringCellValue());
}
}
}

Зареждане…
Отказ
Запис