Commit f6da0235 authored by 军师中郎将's avatar 军师中郎将

1 从业人员 - 导出 - 模版全局为文本格式

parent b0347b04
......@@ -18,25 +18,8 @@ import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
......@@ -349,55 +332,6 @@ public class ExcelUtil<T>
this.init(null, sheetName, Type.IMPORT);
return exportExcel();
}
/**
* 对list数据源将其里面的数据导入到excel表单 - 只返回 outputstream
*
* @return 结果
*/
public OutputStream importTemplateExcelOutputStream()
{
this.init(null, sheetName, Type.IMPORT);
return exportExcelOutputStream();
}
/**
* 对list数据源将其里面的数据导入到excel表单- 只返回 outputstream
*
* @return 结果
*/
public OutputStream exportExcelOutputStream(){
OutputStream out = null;
try {
// 取出一共有多少个sheet.
double sheetNo = Math.ceil(list.size() / sheetSize);
for (int index = 0; index <= sheetNo; index++) {
createSheet(sheetNo, index);
// 产生一行
Row row = sheet.createRow(0);
int column = 0;
// 写入各个字段的列头名称
for (Object[] os : fields) {
Excel excel = (Excel) os[1];
this.createCell(excel, row, column++);
}
if (Type.EXPORT.equals(type)) {
fillExcelData(index, row);
addStatisticsRow();
}
}
String filename = encodingFilename(sheetName);
out = new FileOutputStream(getAbsoluteFile(filename));
} catch (Exception e) {
log.error("导出Excel异常{}", e.getMessage());
throw new CustomException("导出Excel失败,请联系网站管理员!");
}
return out;
}
/**
* 对list数据源将其里面的数据导入到excel表单
*
......@@ -416,12 +350,15 @@ public class ExcelUtil<T>
// 产生一行
Row row = sheet.createRow(0);
int column = 0;
int column = 0,tempColumn = 0;
// 写入各个字段的列头名称
for (Object[] os : fields)
{
Excel excel = (Excel) os[1];
this.createCell(excel, row, column++);
// 设置列默认格式
this.createDefaultColumnStyle(wb, tempColumn++);
}
if (Type.EXPORT.equals(type))
{
......@@ -504,6 +441,7 @@ public class ExcelUtil<T>
// 写入各条记录,每条记录对应excel表中的一行
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style = wb.createCellStyle();
DataFormat excelFormat = wb.createDataFormat();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderRight(BorderStyle.THIN);
......@@ -518,6 +456,7 @@ public class ExcelUtil<T>
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short) 10);
style.setFont(dataFont);
style.setDataFormat(excelFormat.getFormat("@"));
styles.put("data", style);
style = wb.createCellStyle();
......@@ -575,6 +514,25 @@ public class ExcelUtil<T>
return cell;
}
/**
* 创建默认列样式
*
* @param wb 工作薄对象
* @param column 列
*/
private void createDefaultColumnStyle(Workbook wb, int column)
{
// 设置默认输入类型为文本
CellStyle style = wb.createCellStyle();
DataFormat excelFormat = wb.createDataFormat();
// 自动换行
style.setWrapText(true);
// 文本格式
style.setDataFormat(excelFormat.getFormat("@"));
sheet.setDefaultColumnStyle(column, style);
}
/**
* 设置单元格信息
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment