JAVA怎么向Excel中写入批量数据?

JAVA怎么向Excel中写入批量数据?,第1张

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\x0aNumberFormat formatter = NumberFormat.getNumberInstance()\x0d\x0aformatter.setMaximumFractionDigits(8)\x0d\x0a//合计信息\x0d\x0aDouble[] sum = new Double[len]\x0d\x0aObject[] s = null\x0d\x0afor(int i = 0i s = exList.get(i)\x0d\x0arow = sheet.createRow(i + n)\x0d\x0a// 创建数据行\x0d\x0afor(int j = 0j cell = 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 = 0i cell = 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核心代码,我项目上使用的

使用jxl这个包导入这个包下的importjxl.Workbookimportjxl.write.Labelimportjxl.write.WritableSheetimportjxl.write.WritableWorkbook然后Filef=newFile("d:/view.xls")//获得文件WritableWorkbookwb=Workbook.createWorkbook(f)//可以读写的workbookWritableSheets=wb.createSheet("第一页",0)//workbook中的sheet,就是在excel下面那个sheet1,sheet2.这个方法表名使用第一个sheet并且命名为"第一页"Labellable=null//label就是某一个小单元格2层循环,给每个labellabel=newLabel(列,行,值)//都是从0开始的.就是给第几列地几行的labe里写东西s.addCell(l)//把这个lable加入到sheet中最后wb.write()wb.close()读的也差不多.你百度一下jxl,到处都是例子的哇望采纳


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11823816.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存