中易网

Java-怎么才能用Poi打开已有的excel文件,然后追加一些单元格阿?

答案:1  悬赏:40  
解决时间 2021-01-22 15:01
Java-怎么才能用Poi打开已有的excel文件,然后追加一些单元格阿?
最佳答案
String filename = "excel名字.xls";
res.setHeader("Content-Type", "application/vnd.ms-excel");
res.setHeader("ContentDisposition", "attachment;filename="
+ new String(filename.getBytes("MS932"), "ISO-8859-1"));
调用一个方法,输出要输出的值,hm是个map
outExcel(hm, res.getOutputStream());
别看下面的方法代码多,其实有用的就几行,主要就是控制row和cell,设置单元格样式,赋值。

private void outExcel(Map hm, ServletOutputStream out) throws IOException, Exception {
String excelPath = excel模板路径;
excelPath = new String(excelPath.getBytes("iso-8859-1"), "utf-8");
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(excelPath));
HSSFWorkbook wb = new HSSFWorkbook(fs);

HSSFSheet sheet1 = wb.getSheetAt(0);
wb.setSheetName(0, "SheetName");
HSSFPrintSetup ps1 = sheet1.getPrintSetup();
ps1.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);

HSSFRow row = sheet1.getRow((short) (1));
HSSFCell cell = row.getCell((short) (1));
String dataNumber = (String) hm.get("dataNumber");
cell.setCellValue(new HSSFRichTextString(dataNumber));
String birthAddress = (String) hm.get("birthAddress");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(birthAddress));
String acceptDate = (String) hm.get("acceptDate");
row = sheet1.getRow((short) (2));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(acceptDate));
String usedistrictCode = (String) hm.get("usedistrictCode");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(usedistrictCode));
String allegationZipCode = (String) hm.get("allegationZipCode");
row = sheet1.getRow((short) (3));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(allegationZipCode));
String birthName = (String) hm.get("birthName");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(birthName));
String allegationAddress = (String) hm.get("allegationAddress");
row = sheet1.getRow((short) (4));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(allegationAddress));
String birthTellNumber = (String) hm.get("birthTellNumber");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(birthTellNumber));
String allegationName = (String) hm.get("allegationName");
row = sheet1.getRow((short) (5));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(allegationName));
// 敪惗尮娆庬僐乕僪
String industrialCode = (String) hm.get("industrialCode");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(industrialCode));
// 怽棫幰背榖斣崋
String allegationTellNumber = (String) hm.get("allegationTellNumber");
row = sheet1.getRow((short) (6));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(allegationTellNumber));
// 敪惗尮巤愝摍
String birthFacilitiesetc = (String) hm.get("birthFacilitiesetc");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(birthFacilitiesetc));
// 怽棫撪梕
String allegation = (String) hm.get("allegation");
row = sheet1.getRow((short) (7));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(allegation));
// 挷嵏巜摫撪梕
String guidance = (String) hm.get("guidance");
row = sheet1.getRow((short) (8));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(guidance));
// 应掕擭宁担1
String sokuDate1 = (String) hm.get("sokuDate1");
row = sheet1.getRow((short) (9));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(sokuDate1));
// 应掕擭宁担2
String sokuDate2 = (String) hm.get("sokuDate2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(sokuDate2));
// 应掕擭宁担3
String sokuDate3 = (String) hm.get("sokuDate3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(sokuDate3));
// 应掕擭宁担4
String sokuDate4 = (String) hm.get("sokuDate4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(sokuDate4));
// 应掕擭宁担5
String sokuDate5 = (String) hm.get("sokuDate5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(sokuDate5));
// 应掕擭宁担6
String sokuDate6 = (String) hm.get("sokuDate6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(sokuDate6));
// 应掕帪岗1
String sokuTime1 = (String) hm.get("sokuTime1");
row = sheet1.getRow((short) (10));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(sokuTime1));
// 应掕帪岗2
String sokuTime2 = (String) hm.get("sokuTime2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(sokuTime2));
// 应掕帪岗3
String sokuTime3 = (String) hm.get("sokuTime3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(sokuTime3));
// 应掕帪岗4
String sokuTime4 = (String) hm.get("sokuTime4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(sokuTime4));
// 应掕帪岗5
String sokuTime5 = (String) hm.get("sokuTime5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(sokuTime5));
// 应掕帪岗6
String sokuTime6 = (String) hm.get("sokuTime6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(sokuTime6));
// 帋桠斣崋1
String shiryoNumber1 = (String) hm.get("shiryoNumber1");
row = sheet1.getRow((short) (11));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(shiryoNumber1));
// 帋桠斣崋2
String shiryoNumber2 = (String) hm.get("shiryoNumber2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(shiryoNumber2));
// 帋桠斣崋3
String shiryoNumber3 = (String) hm.get("shiryoNumber3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(shiryoNumber3));
// 帋桠斣崋4
String shiryoNumber4 = (String) hm.get("shiryoNumber4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(shiryoNumber4));
// 帋桠斣崋5
String shiryoNumber5 = (String) hm.get("shiryoNumber5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(shiryoNumber5));
// 帋桠斣崋6
String shiryoNumber6 = (String) hm.get("shiryoNumber6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(shiryoNumber6));
// 婥壏1
String temp1 = (String) hm.get("temp1");
row = sheet1.getRow((short) (12));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(temp1));
// 婥壏2
String temp2 = (String) hm.get("temp2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(temp2));
// 婥壏3
String temp3 = (String) hm.get("temp3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(temp3));
// 婥壏4
String temp4 = (String) hm.get("temp4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(temp4));
// 婥壏5
String temp5 = (String) hm.get("temp5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(temp5));
// 婥壏6
String temp6 = (String) hm.get("temp6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(temp6));
// 幖搙1
String humid1 = (String) hm.get("humid1");
row = sheet1.getRow((short) (13));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(humid1));
// 幖搙2
String humid2 = (String) hm.get("humid2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(humid2));
// 幖搙3
String humid3 = (String) hm.get("humid3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(humid3));
// 幖搙4
String humid4 = (String) hm.get("humid4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(humid4));
// 幖搙5
String humid5 = (String) hm.get("humid5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(humid5));
// 幖搙6
String humid6 = (String) hm.get("humid6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(humid6));
// 晽岦1
String windCode1 = (String) hm.get("windCode1");
row = sheet1.getRow((short) (14));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(windCode1));
// 晽岦2
String windCode2 = (String) hm.get("windCode2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(windCode2));
String windCode3 = (String) hm.get("windCode3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(windCode3));
String windCode4 = (String) hm.get("windCode4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(windCode4));
String windCode5 = (String) hm.get("windCode5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(windCode5));
String windCode6 = (String) hm.get("windCode6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(windCode6));
String windSpeed1 = (String) hm.get("windSpeed1");
row = sheet1.getRow((short) (15));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(windSpeed1));
String windSpeed2 = (String) hm.get("windSpeed2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(windSpeed2));
String windSpeed3 = (String) hm.get("windSpeed3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(windSpeed3));
String windSpeed4 = (String) hm.get("windSpeed4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(windSpeed4));
String windSpeed5 = (String) hm.get("windSpeed5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(windSpeed5));
String windSpeed6 = (String) hm.get("windSpeed6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(windSpeed6));
String pick1 = (String) hm.get("pick1");
row = sheet1.getRow((short) (16));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(pick1));
String pick2 = (String) hm.get("pick2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(pick2));
String pick3 = (String) hm.get("pick3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(pick3));
String pick4 = (String) hm.get("pick4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(pick4));
String pick5 = (String) hm.get("pick5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(pick5));
String pick6 = (String) hm.get("pick6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(pick6));
String smell1 = (String) hm.get("smell1");
row = sheet1.getRow((short) (17));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(smell1));
String smell2 = (String) hm.get("smell2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(smell2));
String smell3 = (String) hm.get("smell3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(smell3));
String smell4 = (String) hm.get("smell4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(smell4));
String smell5 = (String) hm.get("smell5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(smell5));
String smell6 = (String) hm.get("smell6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(smell6));
wb.write(out);
out.close();
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
广州博雅酒店退房时间
博士全奖为何签证被拒?
玻尿酸丰下巴要多少钱阿?最好是北京地区的价
绿康王健康洗衣(东兴店)地址在什么地方,想过
起搏器能否解决心衰和脑梗现状
怎样观看守望先锋比赛
请问可以用密度计测量油漆的粘稠度吗
国庆长假 想出去玩。可又怕人太多 想找个人少
正弦波逆变器的过流保护电路设计
我想问下小孩房间什么颜色好看
硚口区商务和旅游局怎么去啊,我要去那办事
如何掌握与客户沟通技巧
单片机与电脑串口通信故障?
打压簧线材不好怎样才能打得稳
请问谁了解夏普65寸液晶电视报价
推荐资讯
小孩抽动症,是怎么回事
九阳按键电磁炉失灵了该怎么去维修
微信红包怎么使用
被太阳晒伤后怎么办?
五月份去哪里旅游好 五月份适合去哪旅游
什么词语描写年青人,什么都不懂,四个字的
建筑工程设计甲级资质可以进行室内精装修设计
求邓丽欣,侧田,郑中基他们每个拍过的电影!
久保田688q收割机倒车掉履带是怎么会事?
两个10两个6.怎么得24
脚干脚裂怎么办 有什么好的办法能治吗
如何放大直流电路中的电流
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?