优化导出excel的功能,解决数字展示的问题

This commit is contained in:
jishenghua
2025-05-21 15:47:49 +08:00
parent 7b2d078382
commit bb42839130
4 changed files with 32 additions and 24 deletions

View File

@@ -513,28 +513,28 @@ public class MaterialService {
} }
String[] names = StringUtil.listToStringArray(nameList); String[] names = StringUtil.listToStringArray(nameList);
String title = "商品信息"; String title = "商品信息";
List<String[]> objects = new ArrayList<>(); List<Object[]> objects = new ArrayList<>();
if (null != dataList) { if (null != dataList) {
for (MaterialVo4Unit m : dataList) { for (MaterialVo4Unit m : dataList) {
String[] objs = new String[names.length]; Object[] objs = new Object[names.length];
objs[0] = m.getName(); objs[0] = m.getName();
objs[1] = m.getStandard(); objs[1] = m.getStandard();
objs[2] = m.getModel(); objs[2] = m.getModel();
objs[3] = m.getColor(); objs[3] = m.getColor();
objs[4] = m.getBrand(); objs[4] = m.getBrand();
objs[5] = m.getCategoryName(); objs[5] = m.getCategoryName();
objs[6] = m.getWeight() == null ? "" : m.getWeight().setScale(3, BigDecimal.ROUND_HALF_UP).toString(); objs[6] = m.getWeight() == null ? "" : m.getWeight().setScale(3, BigDecimal.ROUND_HALF_UP);
objs[7] = m.getExpiryNum() == null ? "" : m.getExpiryNum().toString(); objs[7] = m.getExpiryNum() == null ? "" : m.getExpiryNum();
objs[8] = m.getCommodityUnit(); objs[8] = m.getCommodityUnit();
objs[9] = otherMaterialMap.get(m.getId()) == null ? "" : otherMaterialMap.get(m.getId()).getCommodityUnit(); objs[9] = otherMaterialMap.get(m.getId()) == null ? "" : otherMaterialMap.get(m.getId()).getCommodityUnit();
objs[10] = m.getmBarCode(); objs[10] = m.getmBarCode();
objs[11] = otherMaterialMap.get(m.getId()) == null ? "" : otherMaterialMap.get(m.getId()).getBarCode(); objs[11] = otherMaterialMap.get(m.getId()) == null ? "" : otherMaterialMap.get(m.getId()).getBarCode();
objs[12] = m.getRatio() == null ? "" : m.getRatio().toString(); objs[12] = m.getRatio() == null ? "" : m.getRatio();
objs[13] = m.getSku(); objs[13] = m.getSku();
objs[14] = m.getPurchaseDecimal() == null ? "" : m.getPurchaseDecimal().setScale(3, BigDecimal.ROUND_HALF_UP).toString(); objs[14] = m.getPurchaseDecimal() == null ? "" : m.getPurchaseDecimal().setScale(3, BigDecimal.ROUND_HALF_UP);
objs[15] = m.getCommodityDecimal() == null ? "" : m.getCommodityDecimal().setScale(3, BigDecimal.ROUND_HALF_UP).toString(); objs[15] = m.getCommodityDecimal() == null ? "" : m.getCommodityDecimal().setScale(3, BigDecimal.ROUND_HALF_UP);
objs[16] = m.getWholesaleDecimal() == null ? "" : m.getWholesaleDecimal().setScale(3, BigDecimal.ROUND_HALF_UP).toString(); objs[16] = m.getWholesaleDecimal() == null ? "" : m.getWholesaleDecimal().setScale(3, BigDecimal.ROUND_HALF_UP);
objs[17] = m.getLowDecimal() == null ? "" : m.getLowDecimal().setScale(3, BigDecimal.ROUND_HALF_UP).toString(); objs[17] = m.getLowDecimal() == null ? "" : m.getLowDecimal().setScale(3, BigDecimal.ROUND_HALF_UP);
objs[18] = m.getEnabled() ? "1" : "0"; objs[18] = m.getEnabled() ? "1" : "0";
objs[19] = m.getEnableSerialNumber(); objs[19] = m.getEnableSerialNumber();
objs[20] = m.getEnableBatchNumber(); objs[20] = m.getEnableBatchNumber();
@@ -548,7 +548,7 @@ public class MaterialService {
int i = 27; int i = 27;
for(Depot depot: depotList) { for(Depot depot: depotList) {
BigDecimal number = misMap.get(m.getId() + "_" + depot.getId()); BigDecimal number = misMap.get(m.getId() + "_" + depot.getId());
objs[i] = number == null ? "0" : number.setScale(2, BigDecimal.ROUND_HALF_UP).toString(); objs[i] = number == null ? BigDecimal.ZERO : number.setScale(2, BigDecimal.ROUND_HALF_UP);
i++; i++;
} }
objects.add(objs); objects.add(objs);

View File

@@ -559,10 +559,10 @@ public class SupplierService {
//会员 //会员
String[] names = {"会员卡号*", "联系人", "手机号码", "联系电话", "电子邮箱", "备注", "排序", "状态*"}; String[] names = {"会员卡号*", "联系人", "手机号码", "联系电话", "电子邮箱", "备注", "排序", "状态*"};
String title = "信息内容"; String title = "信息内容";
List<String[]> objects = new ArrayList<String[]>(); List<Object[]> objects = new ArrayList<>();
if (null != dataList) { if (null != dataList) {
for (Supplier s : dataList) { for (Supplier s : dataList) {
String[] objs = new String[names.length]; Object[] objs = new Object[names.length];
objs[0] = s.getSupplier(); objs[0] = s.getSupplier();
objs[1] = s.getContacts(); objs[1] = s.getContacts();
objs[2] = s.getTelephone(); objs[2] = s.getTelephone();
@@ -588,10 +588,10 @@ public class SupplierService {
String[] names = {"名称*", "联系人", "手机号码", "联系电话", "电子邮箱", "传真", beginNeedStr, String[] names = {"名称*", "联系人", "手机号码", "联系电话", "电子邮箱", "传真", beginNeedStr,
"纳税人识别号", "税率(%)", "开户行", "账号", "地址", "备注", "排序", "状态*"}; "纳税人识别号", "税率(%)", "开户行", "账号", "地址", "备注", "排序", "状态*"};
String title = "信息内容"; String title = "信息内容";
List<String[]> objects = new ArrayList<String[]>(); List<Object[]> objects = new ArrayList<>();
if (null != dataList) { if (null != dataList) {
for (Supplier s : dataList) { for (Supplier s : dataList) {
String[] objs = new String[names.length]; Object[] objs = new Object[names.length];
objs[0] = s.getSupplier(); objs[0] = s.getSupplier();
objs[1] = s.getContacts(); objs[1] = s.getContacts();
objs[2] = s.getTelephone(); objs[2] = s.getTelephone();
@@ -599,12 +599,12 @@ public class SupplierService {
objs[4] = s.getEmail(); objs[4] = s.getEmail();
objs[5] = s.getFax(); objs[5] = s.getFax();
if(("客户").equals(s.getType())) { if(("客户").equals(s.getType())) {
objs[6] = s.getBeginNeedGet() == null? "" : s.getBeginNeedGet().setScale(2,BigDecimal.ROUND_HALF_UP).toString(); objs[6] = s.getBeginNeedGet() == null? "" : s.getBeginNeedGet().setScale(2,BigDecimal.ROUND_HALF_UP);
} else if(("供应商").equals(s.getType())) { } else if(("供应商").equals(s.getType())) {
objs[6] = s.getBeginNeedPay() == null? "" : s.getBeginNeedPay().setScale(2,BigDecimal.ROUND_HALF_UP).toString(); objs[6] = s.getBeginNeedPay() == null? "" : s.getBeginNeedPay().setScale(2,BigDecimal.ROUND_HALF_UP);
} }
objs[7] = s.getTaxNum(); objs[7] = s.getTaxNum();
objs[8] = s.getTaxRate() == null? "" : s.getTaxRate().setScale(2,BigDecimal.ROUND_HALF_UP).toString(); objs[8] = s.getTaxRate() == null? "" : s.getTaxRate().setScale(2,BigDecimal.ROUND_HALF_UP);
objs[9] = s.getBankName(); objs[9] = s.getBankName();
objs[10] = s.getAccountNumber(); objs[10] = s.getAccountNumber();
objs[11] = s.getAddress(); objs[11] = s.getAddress();

View File

@@ -648,14 +648,14 @@ public class SystemConfigService {
public void exportExcelByParam(String title, String head, String tip, JSONArray arr, HttpServletResponse response) throws Exception { public void exportExcelByParam(String title, String head, String tip, JSONArray arr, HttpServletResponse response) throws Exception {
List<String> nameList = StringUtil.strToStringList(head); List<String> nameList = StringUtil.strToStringList(head);
String[] names = StringUtil.listToStringArray(nameList); String[] names = StringUtil.listToStringArray(nameList);
List<String[]> objects = new ArrayList<>(); List<Object[]> objects = new ArrayList<>();
if (null != arr) { if (null != arr) {
for (Object object: arr) { for (Object object: arr) {
List<Object> list = (List<Object>) object; List<Object> list = (List<Object>) object;
String[] objs = new String[names.length]; Object[] objs = new Object[names.length];
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
if(null != list.get(i)) { if(null != list.get(i)) {
objs[i] = list.get(i).toString(); objs[i] = list.get(i);
} }
} }
objects.add(objs); objects.add(objs);

View File

@@ -1,7 +1,7 @@
package com.jsh.erp.utils; package com.jsh.erp.utils;
import java.io.*; import java.io.*;
import java.text.SimpleDateFormat; import java.math.BigDecimal;
import java.util.*; import java.util.*;
import jxl.*; import jxl.*;
@@ -110,8 +110,9 @@ public class ExcelUtils {
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static File exportObjectsOneSheet(String fileName, String tip, public static File exportObjectsOneSheet(String fileName, String tip,
String[] names, String title, List<String[]> objects) throws Exception { String[] names, String title, List<Object[]> objects) throws Exception {
File excelFile = new File("/opt/"+ fileName); File excelFile = new File("/opt/"+ fileName);
WritableWorkbook wtwb = Workbook.createWorkbook(excelFile); WritableWorkbook wtwb = Workbook.createWorkbook(excelFile);
WritableSheet sheet = wtwb.createSheet(title, 0); WritableSheet sheet = wtwb.createSheet(title, 0);
@@ -157,9 +158,16 @@ public class ExcelUtils {
// 其余行依次写入数据 // 其余行依次写入数据
int rowNum = 2; int rowNum = 2;
for (int j = 0; j < objects.size(); j++) { for (int j = 0; j < objects.size(); j++) {
String[] obj = objects.get(j); Object[] obj = objects.get(j);
for (int h = 0; h < obj.length; h++) { for (int h = 0; h < obj.length; h++) {
sheet.addCell(new Label(h, rowNum, obj[h], format)); if(obj[h] instanceof String) {
sheet.addCell(new Label(h, rowNum, obj[h].toString(), format));
} else if(obj[h] instanceof BigDecimal || obj[h] instanceof Double || obj[h] instanceof Integer || obj[h] instanceof Long) {
sheet.addCell(new jxl.write.Number(h, rowNum, Double.parseDouble(obj[h].toString()), format));
} else {
String cont = obj[h]!=null?obj[h].toString():"";
sheet.addCell(new Label(h, rowNum, cont, format));
}
} }
rowNum = rowNum + 1; rowNum = rowNum + 1;
} }