您的位置:

java实现导入excel数据(java导入excel数据到数据库)

本文目录一览:

java怎么批量导入excel数据

两种方案:1.可以对数据库中的这张表进行本地缓存处理,验证时调用缓存进行匹配验证。2.用程序生成需要导入excel的数据模板,在模板里将要验证的这一列做成下拉框。此模板条件下的excel数据文件批量导入时即不用校验。具体采用哪种视你的应

如何在Java中导入Excel表数据

使用jxl这个包导入这个包下的importjxl.Workbook;importjxl.write.Label;importjxl.write.WritableSheet;importjxl.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,到处都是例子的哇望采纳

如何在java程序中导入excel数据

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.text.DecimalFormat;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Date;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ReadExcel2 {

/**

* @param args

* @throws IOException

* @throws FileNotFoundException

*/

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

File file = new File("src/test.xls");

String[][] data = getData(file, 0);

printStringArray(data);

}

public static void printStringArray(String[][] data){

for(int i =0; i data.length; i++){

for(int j=0; j data[i].length; j++){

System.out.print(data[i][j]+"\t");

}

System.out.print("\n");

}

}

/**

*

* 读取Excel的内容,第一维数组存储的是一行中格列的值,二维数组存储的是多少个行

* @param file 读取数据的源Excel

* @param ignoreRows 读取数据忽略的行数,比喻行头不需要读入 忽略的行数为1

* @return 读出的Excel中数据的内容

* @throws FileNotFoundException

* @throws IOException

*/

public static String[][] getData(File file, int ignoreRows)

throws FileNotFoundException, IOException {

ListString[] result = new ArrayListString[]();

int rowSize = 0;

BufferedInputStream in = new BufferedInputStream(new FileInputStream(

file));

// 打开HSSFWorkbook

POIFSFileSystem fs = new POIFSFileSystem(in);

HSSFWorkbook wb = new HSSFWorkbook(fs);

HSSFCell cell = null;

for (int sheetIndex = 0; sheetIndex wb.getNumberOfSheets(); sheetIndex++) {

HSSFSheet st = wb.getSheetAt(sheetIndex);

// 第一行为标题,不取

for (int rowIndex = ignoreRows; rowIndex = st.getLastRowNum(); rowIndex++) {

HSSFRow row = st.getRow(rowIndex);

if (row == null) {

continue;

}

int tempRowSize = row.getLastCellNum() + 1;

if (tempRowSize rowSize) {

rowSize = tempRowSize;

}

String[] values = new String[rowSize];

Arrays.fill(values, "");

boolean hasValue = false;

for (short columnIndex = 0; columnIndex = row.getLastCellNum(); columnIndex++) {

String value = "";

cell = row.getCell(columnIndex);

if (cell != null) {

// 注意:一定要设成这个,否则可能会出现乱码

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

switch (cell.getCellType()) {

case HSSFCell.CELL_TYPE_STRING:

value = cell.getStringCellValue();

break;

case HSSFCell.CELL_TYPE_NUMERIC:

if (HSSFDateUtil.isCellDateFormatted(cell)) {

Date date = cell.getDateCellValue();

if (date != null) {

value = new SimpleDateFormat("yyyy-MM-dd")

.format(date);

} else {

value = "";

}

} else {

value = new DecimalFormat("0").format(cell

.getNumericCellValue());

}

break;

case HSSFCell.CELL_TYPE_FORMULA:

// 导入时如果为公式生成的数据则无值

if (!cell.getStringCellValue().equals("")) {

value = cell.getStringCellValue();

} else {

value = cell.getNumericCellValue() + "";

}

break;

case HSSFCell.CELL_TYPE_BLANK:

break;

case HSSFCell.CELL_TYPE_ERROR:

value = "";

break;

case HSSFCell.CELL_TYPE_BOOLEAN:

value = (cell.getBooleanCellValue() == true ? "Y"

: "N");

break;

default:

value = "";

}

}

if (columnIndex == 0 value.trim().equals("")) {

break;

}

values[columnIndex] = rightTrim(value);

hasValue = true;

}

if (hasValue) {

result.add(values);

}

}

}

in.close();

String[][] returnArray = new String[result.size()][rowSize];

for (int i = 0; i returnArray.length; i++) {

returnArray[i] = (String[]) result.get(i);

}

return returnArray;

}

/**

*

* 去掉字符串右边的空格

* @param str 要处理的字符串

* @return 处理后的字符串

*/

public static String rightTrim(String str) {

if (str == null) {

return "";

}

int length = str.length();

for (int i = length - 1; i = 0; i--) {

if (str.charAt(i) != 0x20) {

break;

}

length--;

}

return str.substring(0, length);

}

}

java使用什么技术实现excel数据的批量导入导出

java使用第三方工具包POI技术实现excel数据的批量导入导出。

举例如下:

1、下载apache的相关jar包。poi-ooxml-3.6.jar xmlbeans-2.3.0.jar等,如图:

2、编写相关的读写类

  /**

* 读取xls文件内容

*/

private

ListXlsDto readXls() throws

IOException {

InputStream is = new

FileInputStream("test.xls");

HSSFWorkbook hssfWorkbook = new

HSSFWorkbook(is);

XlsDto xlsDto = null;

ListXlsDto list = new

ArrayListXlsDto();

// 循环工作表Sheet

for

(int numSheet = 0; numSheet hssfWorkbook.getNumberOfSheets(); numSheet++) {

HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);

if

(hssfSheet == null) {

continue;

}

// 循环行Row

for

(int rowNum = 1; rowNum = hssfSheet.getLastRowNum(); rowNum++) {

HSSFRow hssfRow = hssfSheet.getRow(rowNum);

if

(hssfRow == null) {

continue;

}

xlsDto = new

XlsDto();

// 循环列Cell

// 0学号 1姓名 2学院 3课程名 4 成绩

// for (int cellNum = 0; cellNum =4; cellNum++) {

HSSFCell xh = hssfRow.getCell(0);

if

(xh == null) {

continue;

}

xlsDto.setXh(getValue(xh));

HSSFCell xm = hssfRow.getCell(1);

if

(xm == null) {

continue;

}

xlsDto.setXm(getValue(xm));

HSSFCell yxsmc = hssfRow.getCell(2);

if

(yxsmc == null) {

continue;

}

xlsDto.setYxsmc(getValue(yxsmc));

HSSFCell kcm = hssfRow.getCell(3);

if

(kcm == null) {

continue;

}

xlsDto.setKcm(getValue(kcm));

HSSFCell cj = hssfRow.getCell(4);

if

(cj == null) {

continue;

}

xlsDto.setCj(Float.parseFloat(getValue(cj)));

list.add(xlsDto);

}

}

return

list;

}

3、导出就是输入到一个新的excel文件里面

public void writeXls(ListStudent list, String path) throws Exception {

  if (list == null) {原始数据为空,直接返回

      return;

  }

  int countColumnNum = list.size();//设置列数

  HSSFWorkbook book = new HSSFWorkbook(); //创建工作表对象

  HSSFSheet sheet = book.createSheet("studentSheet");

  // 创建第一行

  HSSFRow firstRow = sheet.createRow(0);

  HSSFCell[] firstCells = new HSSFCell[countColumnNum];

  //创建表头

  String[] options = { "no", "name", "age", "score" };

 //循环数据域

  for (int j = 0; j options.length; j++) {

      firstCells[j] = firstRow.createCell(j);

      firstCells[j].setCellValue(new HSSFRichTextString(options[j]));

  }

  //处理每一个cell的值

  for (int i = 0; i countColumnNum; i++) {

      HSSFRow row = sheet.createRow(i + 1);

      Student student = list.get(i);

      for (int column = 0; column options.length; column++) {

          HSSFCell no = row.createCell(0);

          HSSFCell name = row.createCell(1);

          HSSFCell age = row.createCell(2);

          HSSFCell score = row.createCell(3);

          no.setCellValue(student.getNo());

          name.setCellValue(student.getName());

          age.setCellValue(student.getAge());

          score.setCellValue(student.getScore());

      }

  }

  File file = new File(path);

  OutputStream os = new FileOutputStream(file);

  System.out.println(Common.WRITE_DATA + path);

  book.write(os);

  os.close();

}

如何把java查询出的内容导入到excel表格

java查询出的内容导入到excel表格

/**导出数据为XLS格式

* @param fos

* @param bo

*/

public void writeExcelBo(FileOutputStream fos, java.util.Vector ve)

{

jxl.write.WritableWorkbook wwb;

try

{

wwb= Workbook.createWorkbook(fos);

jxl.write.WritableSheet ws= wwb.createSheet("booksheet", 10);

ws.addCell(new jxl.write.Label(0, 1, "书目ID"));

ws.addCell(new jxl.write.Label(1, 1, "ISBN"));

ws.addCell(new jxl.write.Label(2, 1, "定价"));

ws.addCell(new jxl.write.Label(3, 1, "书名"));

ws.addCell(new jxl.write.Label(4, 1, "原书名"));

ws.addCell(new jxl.write.Label(5, 1, "副题名"));

ws.addCell(new jxl.write.Label(6, 1, "著者"));

ws.addCell(new jxl.write.Label(7, 1, "译者"));

ws.addCell(new jxl.write.Label(8, 1, "版次"));

ws.addCell(new jxl.write.Label(9, 1, "出版地"));

ws.addCell(new jxl.write.Label(10, 1, "出版社"));

ws.addCell(new jxl.write.Label(11, 1, "出版日期"));

ws.addCell(new jxl.write.Label(12, 1, "页数"));

ws.addCell(new jxl.write.Label(13, 1, "书高"));

ws.addCell(new jxl.write.Label(14, 1, "装帧"));

ws.addCell(new jxl.write.Label(15, 1, "丛书名"));

ws.addCell(new jxl.write.Label(16, 1, "一般性附注项"));

ws.addCell(new jxl.write.Label(17, 1, "简介"));

ws.addCell(new jxl.write.Label(18, 1, "主题词"));

ws.addCell(new jxl.write.Label(19, 1, "中图法分类"));

ws.addCell(new jxl.write.Label(20, 1, "更新日期"));

ws.addCell(new jxl.write.Label(21, 1, "本数"));

book=new Book[ve.size()];

for (int i= 0; i ve.size(); i++)

{

book[i]= (Book)ve.get(i);

ws.addCell(new jxl.write.Label(0, i + 2, "" + book[i].getBookId()));

ws.addCell(new jxl.write.Label(1, i + 2, book[i].getIsbn()));

ws.addCell(new jxl.write.Label(2, i + 2, "" + book[i].getPrice()));

ws.addCell(new jxl.write.Label(3, i + 2, book[i].getBookTitle()));

ws.addCell(new jxl.write.Label(4, i + 2, book[i].getOldFilename()));

ws.addCell(new jxl.write.Label(5, i + 2, book[i].getSubTitle()));

ws.addCell(new jxl.write.Label(6, i + 2, book[i].getWriter()));

ws.addCell(new jxl.write.Label(7, i + 2, book[i].getTranscribe()));

ws.addCell(new jxl.write.Label(8, i + 2, "" + book[i].getVersion()));

ws.addCell(new jxl.write.Label(9, i + 2, book[i].getPublishCity()));

ws.addCell(new jxl.write.Label(10, i + 2, book[i].getPublisher()));

ws.addCell(new jxl.write.Label(11, i + 2, book[i].getPublishDate().toString()));

ws.addCell(new jxl.write.Label(12, i + 2, "" + book[i].getPage()));

ws.addCell(new jxl.write.Label(13, i + 2, "" + book[i].getHight()));

ws.addCell(new jxl.write.Label(14, i + 2, book[i].getInstall()));

ws.addCell(new jxl.write.Label(15, i + 2, book[i].getSeries()));

ws.addCell(new jxl.write.Label(16, i + 2, book[i].getNotes()));

ws.addCell(new jxl.write.Label(17, i + 2, book[i].getPrecisnotes()));

ws.addCell(new jxl.write.Label(18, i + 2, book[i].getSubject()));

ws.addCell(new jxl.write.Label(19, i + 2, book[i].getCls().replaceAll("_", "")));

ws.addCell(new jxl.write.Label(20, i + 2, book[i].getUpdatedate().toString()));

ws.addCell(new jxl.write.Label(21, i + 2, "0"));

}

jxl.write.WritableFont wfc=

new jxl.write.WritableFont(

WritableFont.ARIAL,

255,

WritableFont.BOLD,

false,

UnderlineStyle.NO_UNDERLINE,

jxl.format.Colour.BLACK);

jxl.write.WritableCellFormat wcfFC= new jxl.write.WritableCellFormat(wfc);

ws.addCell(new jxl.write.Label(0, 0, "为保证您提交定单的稳定和正确,导入定单时候请勿更改此表格式(请勿更改书目ID,订购本数自行添加!)"));

wwb.write();

//关闭Excel工作薄对象

wwb.close();

} catch (IOException e)

{} catch (RowsExceededException e)

{} catch (WriteException e)

{}

}

//导入EXCEL

if (f.getName().indexOf(".xls") 0)

{

try

{

fis= new FileInputStream(f);

BookBean bob= new BookBean();

UserBean usb= new UserBean();

jxl.Workbook rwb= Workbook.getWorkbook(fis);

jxl.Sheet sh= rwb.getSheet(0);

int rowCount= sh.getRows();

SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy");

book= new Book[rowCount - 1];

for (int i= 1; i rowCount; i++)

{

book[i - 1]= new Book();

jxl.Cell[] ce= sh.getRow(i);

book[i - 1].setIsbn(ce[0].getContents().toString());

book[i - 1].setSeries(ce[1].getContents().toString());

book[i - 1].setBookTitle(ce[2].getContents().toString());

book[i - 1].setWriter(ce[3].getContents().toString());

book[i - 1].setTranscribe(ce[4].getContents().toString());

book[i - 1].setPublisher(ce[5].getContents().toString());

book[i - 1].setPublishDate(sdf.parse(ce[6].getContents().toString(), new ParsePosition(0)));

book[i-1].setVersion(Integer.parseInt(ce[7].getContents().toString()));

book[i-1].setPage(Integer.parseInt(ce[8].getContents().toString()));

book[i-1].setCls(ce[9].getContents().toString());

book[i-1].setPrecisnotes(ce[10].getContents().toString());

book[i-1].setInstall(ce[11].getContents().toString());

book[i-1].setPrice(Float.parseFloat(ce[12].getContents().toString()));

book[i-1].setUserid(usb.getUser().getUserid());

getVector().addElement(book[i - 1]);

}

rwb.close();

fis.close();

} catch (FileNotFoundException e)

{} catch (BiffException e)

{} catch (IOException e)

{} catch (NumberFormatException e)

{

ShowMessage("数据导入失败,请按照本软件要求的EXCEL格式导入定单");

}

}

java中怎么把excel导入数据库

1、利用Excel第三方工具,将Excel文件读取到内存中。使用最简单,方便的工具是apache的poi工具包,自己网上下载  ,使用方法网上一搜一大片。

2、如果是对于特别大的excel(大于20M的话),简单的读取方法就容易内存溢出了,需要采用流式读取的方式,参考 

3、将已读入内存的Excel数据,整理成写数据库的数据结构,然后插入数据库。这部分工作应该不用介绍了,就是基本的数据库操作方法,与excel无关了

具体如下:

1、简介

编程是编写程序的中文简称,就是让计算机代为解决某个问题,对某个计算体系规定一定的运算方式,是计算体系按照该计算方式运行,并最终得到相应结果的过程。

为了使计算机能够理解人的意图,人类就必须将需解决的问题的思路、方法和手段通过计算机能够理解的形式告诉计算机,使得计算机能够根据人的指令一步一步去工作,完成某种特定的任务。这种人和计算体系之间交流的过程就是编程。

2、汇编程序

汇编程序。使用汇编语言编写计算机程序,程序员仍然需要十分熟悉计算机系统的硬件结构,所以从程序设计本身上来看仍然是低效率的、繁琐的。但正是由于汇编语言与计算机硬件系统关系密切,在某些特定的场合,如对时空效率要求很高的系统核心程序以及实时控制程序等,迄今为止汇编语言仍然是十分有效的程序设计工具。

3、执行原理

计算机对除机器语言以外的源程序不能直接识别、理解和执行,都必须通过某种方式转换为计算机能够直接执行的。这种将高级编程硬件程序设计语言编写的源程序转换到机器目标程序的方式有两种:解释方式和编译方式。