今天给各位分享javaweb读取大量excel文件的知识,其中也会对java读取大型excel进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、java中怎样从Excel中读写数据2、java web 怎么导入excel文件3、java怎么读取很大的excel(20w条数据),怎么通过字节流获取到单元格的内容4、java如何读取整个excel文件的内容5、如何使用java自动读取web项目中的excel文件
java中怎样从Excel中读写数据
Java EXCEL API简介
Java Excel是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容、创建新的Excel文件、更新已经存在的Excel文件。使用该API非Windows操作系统也可以通过纯Java应用来处理Excel数据表。因为是使用Java编写的,所以我们在Web应用中可以通过JSP、Servlet来调用API实现对Excel数据表的访问。
应用示例
从Excel文件读取数据表
Java Excel API既可以从本地文件系统的一个文件(.xls),也可以从输入流中读取Excel数据表。读取Excel数据表的第一步是创建Workbook(术语:工作薄),下面的代码片段举例说明了应该如何操作: 需要用到一个开源的jar包,jxl.jar。
File file = new File(“c:\\a.xls”);
InputStream in = new FileInputStream(file);
Workbook workbook = Workbook.getWorkbook(in);
//获取第一张Sheet表
Sheet sheet = workbook.getSheet(0);
//我们既可能通过Sheet的名称来访问它,也可以通过下标来访问它。如果通过下标来访问的话,要注意的一点是下标从0开始,就像数组一样。
//获取第一行,第一列的值
Cell c00 = rs.getCell(0, 0);
String strc00 = c00.getContents();
//获取第一行,第二列的值
Cell c10 = rs.getCell(1, 0);
String strc10 = c10.getContents();
//我们可以通过指定行和列得到指定的单元格Cell对象
Cell cell = sheet.getCell(column, row);
//也可以得到某一行或者某一列的所有单元格Cell对象
Cell[] cells = sheet.getColumn(column);
Cell[] cells2 = sheet.getRow(row);
//然后再取每一个Cell中的值
String content = cell.getContents();
java web 怎么导入excel文件
1.要正确的将Web客户端的Excel文件导入到服务器的数据库中,需要将客户端的Excel文件上传到服务器上。可以使用FileUpload控件完成。2.Excel文件上传到服务器指定的目录中,这里假设是该站点的upfiles目录中。3.使用SQL语句从upfiles目录中的上传Excel文件中读取数据显示或写入数据库。
java怎么读取很大的excel(20w条数据),怎么通过字节流获取到单元格的内容
使用java poi
package webservice;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelController {
@SuppressWarnings(“deprecation”)
public void excel() throws FileNotFoundException, IOException{
String filename = “d:\\excel.xls”;
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
//按名引用excel工作表
// HSSFSheet sheet = workbook.getSheet(“JSP”);
//也可以用以下方式来获取excel的工作表,采用工作表的索引值
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow row ;
HSSFCell cell1;
int rows=sheet.getLastRowNum();
for(int icount=0;icountrows;icount++){
row = sheet.getRow(icount);
int line=row.getPhysicalNumberOfCells();
for(int j=0;jline;j++){
cell1= row.getCell(j);
System.out.println(cell1+”–“+icount+”—“+j);
}
}
//打印读取值
// System.out.println(cell.getStringCellValue());
//新建一输出流
FileOutputStream fout = new FileOutputStream(filename); //PS:filename 是你另存为的路径,不处理直接写入模版文件
//存盘
workbook.write(fout);
fout.flush();
//结束关闭
fout.close();
}
public HSSFCell getCell(HSSFRow row, int index) {
// 取得分发日期单元格
HSSFCell cell = row.getCell(index);
// 如果单元格不存在
if (cell == null) {
// 创建单元格
cell = row.createCell(index);
}
// 返回单元格
return cell;
}
public static void main(String[] args) {
ExcelController ec = new ExcelController();
try {
ec.excel();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
java如何读取整个excel文件的内容
工具:
参考代码及注释如下:
import Java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; public class ReadExcel { public static void readExcel(File file){ try { InputStream inputStream = new FileInputStream(file); String fileName = file.getName(); Workbook wb = null; // poi-3.9.jar 只可以读取2007以下的版本,后缀为:xsl wb = new HSSFWorkbook(inputStream);//解析xls格式 Sheet sheet = wb.getSheetAt(0);//第一个工作表 ,第二个则为1,以此类推… int firstRowIndex = sheet.getFirstRowNum(); int lastRowIndex = sheet.getLastRowNum(); for(int rIndex = firstRowIndex; rIndex = lastRowIndex; rIndex ++){ Row row = sheet.getRow(rIndex); if(row != null){ int firstCellIndex = row.getFirstCellNum(); // int lastCellIndex = row.getLastCellNum(); //此处参数cIndex决定可以取到excel的列数。 for(int cIndex = firstCellIndex; cIndex 3; cIndex ++){ Cell cell = row.getCell(cIndex); String value = “”; if(cell != null){ value = cell.toString(); System.out.print(value+”\t”); } } System.out.println(); } } } catch (FileNotFoundException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } public static void main(String[] args) { File file = new File(“D:/test.xls”); readExcel(file); }}
如何使用java自动读取web项目中的excel文件
package com.bjhz.gcp.common.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
* 导出EXCEL
* @author Herenqing
*
*/
public class WritExcel {
public static final Integer WRIT_TITLE = 1;
public static final Integer NO_WRIT_TITLE = 0;
private static String format = “yyyy-MM-dd HH:mm:ss”;
private static String nullDisplay = “”;
private ListString columnName = new ArrayListString();
private MapString, String columnObject = new HashMapString, String();
private String savePath;
private OutputStream outputStream;
/**
* 构造写入EXCEL的路径
* 使用 put 方法创建导出的内容
* 使用 writ 方法将对象写入EXCEL
* @param savePath
* @throws FileNotFoundException
*/
public WritExcel(String savePath) {
this.savePath = savePath;
}
/**
* 构造写入EXCEL的路径
* 使用 put 方法创建导出的内容
* 使用 writOutputStream 方法将对象写入 HSSFWorkbook
* @param savePath
* @throws FileNotFoundException
*/
/*public WritExcel() {
}*/
/**
* 构建导出顺序及导出字段
* @param key 导出字段名
* @param value 表头
*/
public void put(String key,String value) {
this.columnName.add(key);
this.columnObject.put(key, value);
}
/**
* 写入EXCEL
* @param T 写入EXCEL的对象类型
* @param objects 写入EXCEL的对象
* @param model 写不写表头
* @throws IOException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public T void writ(ListT objects,Integer model) throws IOException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
// 创建一个Excel
HSSFWorkbook book = new HSSFWorkbook();
// 创建一个工作簿
HSSFSheet sheet = book.createSheet();
// 设置工作簿的默认列宽
sheet.setDefaultColumnWidth(30);
Integer start = 0;
if (model == WRIT_TITLE) {
HSSFCellStyle titleStyle = buildTitleStyle(book);
writExcelTitle(sheet,titleStyle);
start = WRIT_TITLE;
}
HSSFCellStyle columnStyle = buildColumnStyle(book);
writExcelColumn(objects,start,sheet,columnStyle);
outputStream = new FileOutputStream(savePath);
book.write(outputStream);
outputStream.close();
}
/**
* 写入EXCEL
* @param T 写入EXCEL的对象类型
* @param objects 写入EXCEL的对象
* @param model 写不写表头
* @throws IOException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
/*public T HSSFWorkbook writOutputStream(ListT objects,Integer model) throws IOException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
// 创建一个Excel
HSSFWorkbook book = new HSSFWorkbook();
// 创建一个工作簿
HSSFSheet sheet = book.createSheet();
// 设置工作簿的默认列宽
sheet.setDefaultColumnWidth((short)30);
Integer start = 0;
if (model == WRIT_TITLE) {
HSSFCellStyle titleStyle = buildTitleStyle(book);
writExcelTitle(sheet,titleStyle);
start = WRIT_TITLE;
}
HSSFCellStyle columnStyle = buildColumnStyle(book);
writExcelColumn(objects,start,sheet,columnStyle);
return book;
}*/
/**
* 使用内容样式
* @return
*/
private HSSFCellStyle buildColumnStyle(HSSFWorkbook book){
// 设置样式
HSSFCellStyle columnStyle = book.createCellStyle();
//titleStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
//titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
columnStyle.setLeftBorderColor(HSSFCellStyle.BORDER_THIN);
columnStyle.setRightBorderColor(HSSFCellStyle.BORDER_THIN);
columnStyle.setTopBorderColor(HSSFCellStyle.BORDER_THIN);
columnStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 设置字体
HSSFFont font = book.createFont();
//font.setColor(HSSFColor.VIOLET.index);
//font.setFontHeight((short)12);
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前样式
columnStyle.setFont(font);
return columnStyle;
}
/**
* 使用表头样式
*/
private HSSFCellStyle buildTitleStyle(HSSFWorkbook book){
// 设置样式
HSSFCellStyle titleStyle = book.createCellStyle();
//titleStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
//titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
titleStyle.setLeftBorderColor(HSSFCellStyle.BORDER_THIN);
titleStyle.setRightBorderColor(HSSFCellStyle.BORDER_THIN);
titleStyle.setTopBorderColor(HSSFCellStyle.BORDER_THIN);
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 设置字体
HSSFFont font = book.createFont();
//font.setColor(HSSFColor.VIOLET.index);
//font.setFontHeight((short)12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前样式
titleStyle.setFont(font);
return titleStyle;
}
/**
* 写入表头
*/
private void writExcelTitle(HSSFSheet sheet,HSSFCellStyle titleStyle) throws IOException {
// 表头
HSSFRow title = sheet.createRow(0);
for (Integer i = 0; i columnName.size(); i++) {
HSSFCell cell = title.createCell(i);
cell.setCellStyle(titleStyle);
HSSFRichTextString value = new HSSFRichTextString(columnObject.get(columnName.get(i)));
cell.setCellValue(value);
}
}
/**
* 写入内容
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
private T void writExcelColumn(ListT objects,Integer start,HSSFSheet sheet,HSSFCellStyle titleStyle) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
for (int i = 0; i objects.size(); i++) {
Object object = objects.get(i);
Class? cls = object.getClass();
HSSFRow column = sheet.createRow(start);
for (int j = 0; j columnName.size(); j++) {
try {
Method method = cls.getMethod(getMethod(columnName.get(j)));
Object invokeResult = method.invoke(object);
String result = returnTypeResult(invokeResult);
HSSFCell cell = column.createCell(j);
cell.setCellStyle(titleStyle);
HSSFRichTextString value = new HSSFRichTextString(result);
cell.setCellValue(value);
} catch (NoSuchMethodException e) {
continue;
}
}
start++;
}
}
/**
* 构建get方法
*/
private static String getMethod(String fieldName){
return “get”+fieldName.substring(0,1).toUpperCase(Locale.CHINA)+fieldName.substring(1);
}
private static String returnTypeResult(Object object){
if (object == null) {
return nullDisplay;
}
if (object.getClass().getName().equals(“java.util.Date”)) {
return formatDate(object);
}else if (object.getClass().getName().equals(“java.sql.Timestamp”)) {
Timestamp timestamp = Timestamp.valueOf(object.toString());
Date date = timestamp;
return formatDate(date);
}else {
return object.toString();
}
}
/**
* formatDate
*/
private static String formatDate(Object date) {
return new SimpleDateFormat(format).format(date);
}
}
javaweb读取大量excel文件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java读取大型excel、javaweb读取大量excel文件的信息别忘了在本站进行查找喔。