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

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

String filename = "顷神excel名字.xls"

res.setHeader("Content-Type", "application/vnd.ms-excel")

res.setHeader("ContentDisposition", "脊段attachmentfilename="

+ 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()

}

package bis.excel

import java.io.File

import java.io.FileInputStream

import java.io.InputStream

import java.util.ArrayList

import java.util.List

import java.util.Map

import java.util.TreeMap

import jxl.Sheet

import jxl.Workbook

public class Excel {

@SuppressWarnings("unchecked")

public List addCust(File file){

List list=new ArrayList()

List list2=new ArrayList()

Workbook rwb=null

try {

List list1=new ArrayList()

InputStream is=new FileInputStream(file)//读取文件(所要导入excel的保存目录,如:f:\\a.xls)

rwb=Workbook.getWorkbook(is)//颂慎创建工作薄

Sheet rs=rwb.getSheet(0)//读取excel中的第一个工作表(默认新建excel下面有sheet1,sheet2,sheet3)

int cellCount=rs.getColumns()//获取Sheet表中所包含的总列数

int rowCount=rs.getRows()//获取Sheet表中所包含的总行数

for(int m=0m<cellCountm++){//将表的第一行数据保存到list1中(列名),即id,name ,age

String cell=rs.getCell(m,0).getContents()

list1.add(cell)

}

for(int i=1i<rowCounti++){//获取值

Map map=new TreeMap()

for(int j=0j<cellCountj++){

map.put(list1.get(j),rs.getCell(j,i).getContents())//将值以键毕碰/值对方式保存到map对象中即(id:1,name:zhangsan,age:18)

}

list.add(map)//将值保存到list中

//System.out.println(list.get(i-1))

}

list2.add(list1)//将表头(id,name,age)保存到list2中

list2.add(list)//将值保存到list2中

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace()

}finally{

rwb.close()

}

return list2

}

public void tosql(){

List list=addCust(file)//file:所要导入excel的保存目录,如:f:\\a.xls

Map map=new HashMap()

String[] values=new String[ls2.size()]//保存id,name,age 值

for (int i =0i <ls2.size()i++) {

map=(Map)ls2.get(i)

String value=""

id=(String)map.get("id")

name=(String)map.get("name")

age=(String)map.get("age")

value=id+""+name+""+age

values[i]=value

}

}

}

这是段读取excel表数据的代码,在tosql方野数敬法中调用addCust方法读取excel表,最后把所有行的id,name,age值保存到了values数组中,也可以保存到类中,如果你会对数据库 *** 作的话,

后面的你自己弄下就行了,不会的话留言,我晚上在告诉你,我现在上班呢,时间有限,只能写这么多了


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存