关于xssfex的信息

hacker|
164

文章目录:

poi如何去写入excel文件

直接全部在action里面写的,这个就不多说了,直接上代码:

public String executeExcel() throws Exception{ String realPath = ServletActionContext.getServletContext().getRealPath("/fileupload");

System.out.println(fileFileName);

String filePath = "";

if(this.file!=null){

File saveFile = new File(new File(realPath),this.fileFileName);

filePath = realPath+"\\"+this.fileFileName;

System.out.println(filePath);

if(!saveFile.getParentFile().exists()){

saveFile.getParentFile().mkdirs(); }

FileUtils.copyFile(file, saveFile); }

this.exlToDB(filePath);

ActionContext.getContext().put("message","导入成功");

return "success"; } //读取excel2007,并把数据插入数据库

public void exlToDB(String filePath){ boolean flag = true;

AllKpi akpi = new AllKpi(); try { // 文件流指向excel文件

FileInputStream fin=new FileInputStream(filePath);

XSSFWorkbook workbook = new XSSFWorkbook(fin);// 创建工作薄

XSSFSheet sheet = workbook.getSheetAt(0);// 得到工作表

XSSFRow row = null;// 对应excel的行

XSSFCell cell = null;// 对应excel的列

int totalRow = sheet.getLastRowNum();// 得到excel的总记录条数

System.out.println(totalRow); // 以下的字段一一对应数据库表的字段

float idd = 0.0f;

String id = "";

String Name = "";

String DEPT_NAME = "";

String Weight = "";

span/spanString ALGORITHM = "";

String text = " ";

//String sql = "insert into DSP_TJ_KPI values(DSP_TJ_KPI_SEQ.nextval,?,?,?,'无',?)";

for (int i = 1; i = totalRow; i++) {

row = sheet.getRow(i);

//System.out.println(row.getCell(0).toString());

if(row.getCell(0) != null !"".equals(row.getCell(0)) row.getCell(1) != null !"".equals(row.getCell(1)) row.getCell(2) != null !"".equals(row.getCell(2)) row.getCell(3) != null !"".equals(row.getCell(3))){

cell = row.getCell((short) 0);

Name = cell.toString();

System.out.println(Name);

cell = row.getCell((short) 1);

Weight = cell.toString();

System.out.println(Weight);

cell = row.getCell((short) 2);

DEPT_NAME = cell.toString();

System.out.println(DEPT_NAME);

cell = row.getCell((short) 3);

ALGORITHM = cell.toString();

System.out.println(ALGORITHM);

akpi.setAllkpiName(Name);

akpi.setAllkpiDeptName(DEPT_NAME);

akpi.setAllkpiWeight(Weight);

akpi.setAlgorithm(ALGORITHM);

akpi.setText(text);

allKpiService.addAllKpi(akpi); //以下注释代码为连接jdbc测试代码块

/*pst = con.prepareStatement(sql);

//pst.setString(1, student_id);

pst.setString

(1, DEPT_NAME);

pst.setString

(2, Name);

pst.setString

(3, Weight);

span/spanpst.setString(4, ALGORITHM);

pst.execute();*/

System.out.println("preparestatement successful"); } }

/*pst.close();

con.close();*/

fin.close(); } catch (FileNotFoundException e)

{

flag = false;

e.printStackTrace();

}

catch (IOException ex)

{

flag = false;

ex.printStackTrace();

}

java excel一般怎样导入oracel数据库

你是说用java读取excel的数据插入数据库? 可以使用第三方插件,下载一个jxl.jar放到工程中;

如果你的excel数据是一行对应一条表数据的话,我这里有个例子你参考下:

public class ReaderExcel {

public static void main(String[] args) throws FileNotFoundException {

Workbook wb = null;

try {

wb = Workbook.getWorkbook(new File("D:/test.xls"));

Sheet rs = wb.getSheet(0);

int column = 3;//列数

for (int i = 0; i rs.getRows(); i++) {

for (int j = 0; j column; j++) {

/**

比如你有3列:用户名、密码、电话

那么这个内循环全部循环完就可以得到一行数据的3个值;

rs.getCell(j, i).getContents().trim()

具体你可以找个excel试试,打印一下看下输出就懂了

*/

System.out.println(rs.getCell(j, i).getContents().trim());

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

记得下载jxl.jar导入项目中哦!!!望采纳!

java通过poi生成excel的版本问题

XSSF不能读取Excel2003以前(包括2003)的版本,

没需要就按你之前的继续,如果在读取前判断文件是2003前的版本还是2007的版本,提供个思路。XSSF和HSSF虽然在不同的包里,但却引用了同一接口Workbook,

Workbook book = null;

try {

book = new XSSFWorkbook(excelFile);

} catch (Exception ex) {

book = new HSSFWorkbook(new FileInputStream(excelFile));

}

各版本的Excel中测试,没有发生异常

3条大神的评论

  • avatar
    访客 2022-09-04 下午 05:26:20

    ;如果你的excel数据是一行对应一条表数据的话,我这里有个例子你参考下:public class ReaderExcel { public static void main(String[] args) throws FileNotFoundException { Workbook

  • avatar
    访客 2022-09-04 下午 01:10:48

    uccess"; } //读取excel2007,并把数据插入数据库 public void exlToDB(String filePath){ boolean fl

  • avatar
    访客 2022-09-04 下午 04:52:24

    rvletActionContext.getServletContext().getRealPath("/fileupload"); System.out.println(fileFi

发表评论