非常风气网www.verywind.cn
首页
JAVA怎么向Excel中写入批量数据?
如题所述
其他回答
第1个回答 2022-11-16
public static void drawExcel(HSSFWorkbook wb, String sheetName, String title, int n, List exlList, int[] index){\x0d\x0aList
exList =(List
)exlList;\x0d\x0aint len = exList.get(0).length;\x0d\x0a// 创建一个sheet表单\x0d\x0aHSSFSheet sheet = wb.createSheet(sheetName);\x0d\x0aRegion region = null;\x0d\x0a//样式\x0d\x0aHSSFCellStyle cellStyle1 = setStyleBorder(wb);\x0d\x0aHSSFCellStyle cellStyle2 = setStyleFontBorder(wb);\x0d\x0a// 创建标题行\x0d\x0aHSSFRow row = sheet.createRow(0);\x0d\x0arow.setHeight((short)500);\x0d\x0a// 创建单元格\x0d\x0aHSSFCell cell = null;\x0d\x0aif(title != null && !"".equals(title)){\x0d\x0aregion = new Region(0, (short)0, 0, (short)(len- 1));\x0d\x0asheet.addMergedRegion(region);\x0d\x0a\x0d\x0acell = row.createCell(0);\x0d\x0a// 标题写入单元格\x0d\x0acell.setCellValue(title);\x0d\x0a cell.setCellStyle(setStyleFontSize(wb, 18));\x0d\x0a}else{\x0d\x0an = n - 1;\x0d\x0a}\x0d\x0a\x0d\x0a NumberFormat formatter = NumberFormat.getNumberInstance();\x0d\x0a formatter.setMaximumFractionDigits(8);\x0d\x0a//合计信息\x0d\x0aDouble[] sum = new Double[len];\x0d\x0aObject[] s = null;\x0d\x0afor(int i = 0; i < exList.size(); i++){\x0d\x0as = exList.get(i);\x0d\x0arow = sheet.createRow(i + n);\x0d\x0a// 创建数据行\x0d\x0afor(int j = 0; j < s.length; j++){\x0d\x0acell = row.createCell(j);\x0d\x0a/***\x0d\x0a* jobin create\x0d\x0a*/\x0d\x0aif(s[j] instanceof Integer || s[j] instanceof Float){\x0d\x0acell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);\x0d\x0a}else{\x0d\x0acell.setCellType(HSSFCell.CELL_TYPE_STRING);\x0d\x0a}\x0d\x0aif(!StringUtils.isNotEmpty(s[j])){\x0d\x0acell.setCellStyle(cellStyle1);\x0d\x0acontinue;\x0d\x0a}\x0d\x0aString[] rs = null;\x0d\x0aint l = 3000;\x0d\x0aif(i == 0 && s[j].toString().indexOf(",") > 0){\x0d\x0ars = s[j].toString().split(",");\x0d\x0acell.setCellValue(rs[0]);\x0d\x0al = Integer.parseInt(rs[1]);\x0d\x0a}else{\x0d\x0acell.setCellValue(s[j].toString());\x0d\x0a}\x0d\x0aif(i == 0){\x0d\x0acell.setCellStyle(cellStyle2);\x0d\x0a//设置列宽\x0d\x0a//if(j == 0){\x0d\x0a sheet.setColumnWidth(j, l);\x0d\x0a//}else if(j == s.length -1){\x0d\x0a// sheet.setColumnWidth(j, 5000);\x0d\x0a//}else{\x0d\x0a// sheet.setColumnWidth(j, 3000);\x0d\x0a//}\x0d\x0a}else{\x0d\x0acell.setCellStyle(cellStyle1);\x0d\x0a//合计统计\x0d\x0aif(index != null){\x0d\x0afor(int in : index){\x0d\x0aif(in == j){\x0d\x0aif(sum[in] == null) sum[in] = 0.0;\x0d\x0asum[in] += Double.parseDouble(s[j].toString());\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a//合计信息\x0d\x0aif(index != null){\x0d\x0aregion = new Region(exList.size() + 1, (short)0, exList.size() + 1, (short)(index[0]-1));\x0d\x0asheet.addMergedRegion(region);\x0d\x0arow = sheet.createRow(exList.size() + n);\x0d\x0afor(int i = 0; i < sum.length; i++){\x0d\x0acell = row.createCell(i);\x0d\x0acell.setCellStyle(cellStyle2);\x0d\x0aif(i == 0){\x0d\x0acell.setCellValue("合计");\x0d\x0a}else if(sum[i] != null){\x0d\x0acell.setCellValue(formatter.format(sum[i]).replace(",", ""));\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a}\x0d\x0a核心代码,我项目上使用的
相关了解……
你可能感兴趣的内容
大家正在搜
python向excel写入数据
vc批量写入Excel
excel写入数据库
Excel如何读取写入数据库
java怎么写入excel
java将数据写入excel
pandas将数据写入excel
php写入数据到数据库
scala写入Excel
本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
©
非常风气网