重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
需要jxl的包
成都创新互联致力于互联网品牌建设与网络营销,包括网站制作、做网站、SEO优化、网络推广、整站优化营销策划推广、电子商务、移动互联网营销等。成都创新互联为不同类型的客户提供良好的互联网应用定制及解决方案,成都创新互联核心团队十多年专注互联网开发,积累了丰富的网站经验,为广大企业客户提供一站式企业网站建设服务,在网站建设行业内树立了良好口碑。
//打开文件
Workbook book = Workbook.getWorkbook(new File(path)) ;
//取得第一个sheet
Sheet sheet = book.getSheet(0);
int rows = sheet.getRows();
//i和j是你要的行和列
Cell [] cell = sheet.getRow(i);
Cell cloumn_cell = sheet.getCell(j, i);
//str是你的内容
String str = cloumn_cell.getContents();
方法有点老了 不知道能不能帮到你
使用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程序添加spire.xls.jar依赖
import com.spire.xls.*;
public class ReadExcel {
public static void main(String[] args) {
//创建Workbook对象
Workbook wb = new Workbook();
//加载一个Excel文档
wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\test.xlsx");
//获取第一个工作表
Worksheet sheet = wb.getWorksheets().get(0);
//遍历工作表的每一行
for (int i = 1; i sheet.getLastRow() + 1; i++) {
//遍历工作的每一列
for (int j = 1; j sheet.getLastColumn() + 1; j++) {
//输出指定单元格的数据
System.out.print(sheet.get(i,j).getText());
System.out.print("\t");
}
System.out.print("\n");
}
}
}
The server encountered an internal error () that prevented it from fulfilling this request.
package Read;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import bean.define;
public class readxlsx {
public ListListString readxl(String path) throws IOException{
InputStream is = new FileInputStream(path);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
ListListString result = new ArrayListListString();
/*
//Sheet xssfSheet = null;
//循环每一页,并处理当前循环页*/
for (XSSFSheet xssfSheet: xssfWorkbook) {
if (xssfSheet == null)
continue;
//处理当前页,循环读取每一行
for (int rowNum = 1; rowNum = xssfSheet.getLastRowNum(); rowNum++){
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
int minColIx = xssfRow.getFirstCellNum();
int maxColIx = xssfRow.getLastCellNum();
ListString rowList = new ArrayListString();
//Listdefine list = null;
//遍历该行,获取处理每个cell元素
for (int colIx = minColIx; colIx maxColIx; colIx++) {
XSSFCell cell = xssfRow.getCell(colIx);
if(cell == null) {
define define = new define();
XSSFCell id = xssfRow.getCell(0);
XSSFCell what = xssfRow.getCell(1);
define.setId(id.getStringCellValue());
define.setWhat(what.getStringCellValue());
continue;
}
rowList.add(cell.toString());
}
result.add(rowList);
}
}
return result;
}
}
package com.runoob.test;
import java.io.IOException;
import java.util.List;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Dao.dao;
import bean.define;
import Read.readxlsx;
public class adden extends HttpServlet{
private define define;
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
readxlsx readxlsx = new readxlsx();
String path = request.getParameter("path");
PrintWriter out = response.getWriter();
ListListString list = readxlsx.readxl(path);
String id = request.getParameter("id");
String what = request.getParameter("what");
ListString de = new define();
dao d = new dao();
for(int i=0;ilist.size();i++){
de=list.get(i);
if(dao.adden(define)){
response.sendRedirect("index.jsp");
}
else out.println("读取失败");
}
out.flush();
out.close();
}
}
引入poi的jar包,大致如下:
读取代码如下,应该能看得明白吧
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtil2007 {
/**读取excel文件流的指定索引的sheet
* @param inputStream excel文件流
* @param sheetIndex 要读取的sheet的索引
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static XSSFSheet readExcel(InputStream inputStream,int sheetIndex) throws FileNotFoundException, IOException
{
return readExcel(inputStream).getSheetAt(sheetIndex);
}
/**读取excel文件的指定索引的sheet
* @param filePath excel文件路径
* @param sheetIndex 要读取的sheet的索引
* @return
* @throws IOException
* @throws FileNotFoundException
*/
public static XSSFSheet readExcel(String filePath,int sheetIndex) throws FileNotFoundException, IOException
{
return readExcel(filePath).getSheetAt(sheetIndex);
}
/**读取excel文件的指定索引的sheet
* @param filePath excel文件路径
* @param sheetName 要读取的sheet的名称
* @return
* @throws IOException
* @throws FileNotFoundException
*/
public static XSSFSheet readExcel(String filePath,String sheetName) throws FileNotFoundException, IOException
{
return readExcel(filePath).getSheet(sheetName);
}
/**读取excel文件,返回XSSFWorkbook对象
* @param filePath excel文件路径
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static XSSFWorkbook readExcel(String filePath) throws FileNotFoundException, IOException
{
XSSFWorkbook wb=new XSSFWorkbook(new FileInputStream(filePath));
return wb;
}
/**读取excel文件流,返回XSSFWorkbook对象
* @param inputStream excel文件流
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static XSSFWorkbook readExcel(InputStream inputStream) throws FileNotFoundException, IOException
{
XSSFWorkbook wb=new XSSFWorkbook(inputStream);
return wb;
}
/***读取excel中指定的单元格,并返回字符串形式的值
* 1.数字
* 2.字符
* 3.公式(返回的为公式内容,非单元格的值)
* 4.空
* @param st 要读取的sheet对象
* @param rowIndex 行索引
* @param colIndex 列索引
* @param isDate 是否要取的是日期(是则返回yyyy-MM-dd格式的字符串)
* @return
*/
public static String getCellString(XSSFSheet st,int rowIndex,int colIndex,boolean isDate){
String s="";
XSSFRow row=st.getRow(rowIndex);
if(row == null) return "";
XSSFCell cell=row.getCell(colIndex);
if(cell == null) return "";
if (cell.getCellType() == 0) {//数字
if(isDate)s=new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue());
else s = trimPointO(String.valueOf(getStringValue(cell)).trim());
}else if (cell.getCellType() == 1){//字符(excel中的空格,不是全角,也不是半角,不知道是神马,反正就是" "这个)
s=cell.getRichStringCellValue().getString().replaceAll(" ", " ").trim();
// s=cell.getStringCellValue();//07API新增,好像跟上一句一致
}
else if (cell.getCellType() == 2){//公式
s=cell.getCellFormula();
}
else if (cell.getCellType() == 3){//空
s="";
}
return s;
}
/**如果数字以 .0 结尾,则去掉.0
* @param s
* @return
*/
public static String trimPointO(String s) {
if (s.endsWith(".0"))
return s.substring(0, s.length() - 2);
else
return s;
}
/**处理科学计数法和百分比模式的数字单元格
* @param cell
* @return
*/
public static String getStringValue(XSSFCell cell) {
String sValue = null;
short dataFormat = cell.getCellStyle().getDataFormat();
double d = cell.getNumericCellValue();
BigDecimal b = new BigDecimal(Double.toString(d));
//百分比样式的
if (dataFormat == 0xa || dataFormat == 9) {
b=b.multiply(new BigDecimal(100));
//String temp=b.toPlainString();
DecimalFormat df=new DecimalFormat("0.00");//保留两位小数的百分比格式
sValue = df.format(b) + "%";
}else{
sValue = b.toPlainString();
}
return sValue;
}
}