Java-怎么才能用Poi打开已有的excel文件,然后追加一些单元格阿?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java-怎么才能用Poi打开已有的excel文件,然后追加一些单元格阿?相关的知识,希望对你有一定的参考价值。
如题,我只要Poi的例子,不要jxl的
参考技术A 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();
本回答被提问者采纳
以上是关于Java-怎么才能用Poi打开已有的excel文件,然后追加一些单元格阿?的主要内容,如果未能解决你的问题,请参考以下文章
java用jxl读取网页上下载的excel文件,怎么识别?求解答
JAVA编程中用Apache POI 怎么用SXSSFWorkbook对已存在的excel(.xlsx)操作进行写数据操作