java获取excel表的行,java读取excel表格行数据-用java编写代码按照正确格式读取完整excel表格的数......

java 如何一列一列读取excel数据。网上好多资料只...

InputStream inp = new FileInputStream("workbook.xls");

//InputStream inp = new FileInputStream("workbook.xlsx");

Workbook wb = WorkbookFactory.create(inp);

Sheet sheet = wb.getSheetAt(0);

Row r = sheet.getRow(x);

Cell c = r.getCell(y);

int value = c.getNumericCellValue();

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;icount

row = sheet.getRow(icount);

int line=row.getPhysicalNumberOfCells();

for(int j=0;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 poi怎么获取excel单元格的内容?

用jexcel类库

使用方法

Workbook book = Workbook.getWorkbook(new File(DefaultValue.PRJ_ROOT "/data.xls"));

Sheet sheet = book.getSheet(0);

for(int i=1;i

Cell c = sheet.getCell(0,i);

System.out.println(sheet.getCell(1, i).getContents());

}

没错,就这单。赶紧试试吧

用java编写代码按照正确格式读取完整excel表格的数...

工作中用excel一个方法,你还可以通过一些插件导入,代码要你自己了,基本原理如下...

public Object importDoucument(MultipartFile uploadfile)

{

StringBuffer resultMessage = new StringBuffer();

ExcelImport excelImport = new ExcelImport();

Sheet sheet = null;

try

{

// 验证文件 如不出错 返回工作簿

excelImport.verifyExeclFile(uploadfile);

ExcelBean excelBean = excelImport.getExcelBean();

if (null != excelBean)

{

sheet = excelBean.getSheet();

}

//导入excel文件分析整理出list对象

List dataList = getAssessCateRange(sheet, "战略要素名称", "战略要素名称", 2, 1);

int num = 0;

if(dataList.size()>0){

for (StcCoreElements itemStcVO : dataList)

{

StcCoreElements stcCoreElementsVo = nitemStcVO

//*****修改些处

this.save(stcCoreElementsVo);

num;

}

}

resultMessage.append("已成功导入 " num " 条核心要素信息");

}

catch (Exception e)

{

resultMessage.append(e.getMessage());

e.printStackTrace();

}

finally

{

excelImport.close();

}

return resultMessage;

}

private List getAssessCateRange(Sheet sheet, String startName, String endName, int rowNum, int titleRowNum)

{

int[] cateRange = new int[2];

List dataList = new ArrayList();

int lastRowNumber = sheet.getLastRowNum();

Row cateRow = sheet.getRow(rowNum - 1);

Cell cateCell = cateRow.getCell(0);

String cateCellValue = ImportExcelUtil.getCellValue(cateCell, sheet);

if (StringUtils.isNotBlank(cateCellValue))

{

if (StringUtils.startsWith(cateCellValue, startName))

{

cateRange[0] = rowNum titleRowNum;

}

}

String currentCellValue0 = "";

do

{

Row currentRow = sheet.getRow(rowNum);

StcCoreElements info =new StcCoreElements();

Cell currentCell0 = currentRow.getCell(0);

currentCellValue0 = ImportExcelUtil.getCellValue(currentCell0, sheet);

info.setOverallPlan(currentCellValue0);

dataList.add(info);

rowNum ;

} while (rowNum <= lastRowNumber);

return dataList;

}

我想用java来读取Excel文件的某行某列,就是指定读...

工作中用excel一个方法,你还可以通过一些插件导入,代码要你自己了,基本原理如下...

public Object importDoucument(MultipartFile uploadfile)

{

StringBuffer resultMessage = new StringBuffer();

ExcelImport excelImport = new ExcelImport();

Sheet sheet = null;

try

{

// 验证文件 如不出错 返回工作簿

excelImport.verifyExeclFile(uploadfile);

ExcelBean excelBean = excelImport.getExcelBean();

if (null != excelBean)

{

sheet = excelBean.getSheet();

}

//导入excel文件分析整理出list对象

List dataList = getAssessCateRange(sheet, "战略要素名称", "战略要素名称", 2, 1);

int num = 0;

if(dataList.size()>0){

for (StcCoreElements itemStcVO : dataList)

{

StcCoreElements stcCoreElementsVo = nitemStcVO

//*****修改些处

this.save(stcCoreElementsVo);

num;

}

}

resultMessage.append("已成功导入 " num " 条核心要素信息");

}

catch (Exception e)

{

resultMessage.append(e.getMessage());

e.printStackTrace();

}

finally

{

excelImport.close();

}

return resultMessage;

}

private List getAssessCateRange(Sheet sheet, String startName, String endName, int rowNum, int titleRowNum)

{

int[] cateRange = new int[2];

List dataList = new ArrayList();

int lastRowNumber = sheet.getLastRowNum();

Row cateRow = sheet.getRow(rowNum - 1);

Cell cateCell = cateRow.getCell(0);

String cateCellValue = ImportExcelUtil.getCellValue(cateCell, sheet);

if (StringUtils.isNotBlank(cateCellValue))

{

if (StringUtils.startsWith(cateCellValue, startName))

{

cateRange[0] = rowNum titleRowNum;

}

}

String currentCellValue0 = "";

do

{

Row currentRow = sheet.getRow(rowNum);

StcCoreElements info =new StcCoreElements();

Cell currentCell0 = currentRow.getCell(0);

currentCellValue0 = ImportExcelUtil.getCellValue(currentCell0, sheet);

info.setOverallPlan(currentCellValue0);

dataList.add(info);

rowNum ;

} while (rowNum <= lastRowNumber);

return dataList;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。您可以使用第三方库,如Apache POI,读取Excel表格并将其转化为JSON数据。以下是Java代码示例: ```java // 导入依赖 import org.apache.poi.ss.usermodel.*; import org.json.*; import java.io.*; import java.util.*; public class ExcelToJsonConverter { public static void main(String[] args) throws Exception { // 读取Excel文件 Workbook workbook = WorkbookFactory.create(new File("data.xlsx")); // 读取第一个工作表 Sheet sheet = workbook.getSheetAt(0); // 获取表头 Row headerRow = sheet.getRow(0); // 遍历每一 JSONArray rows = new JSONArray(); for (int i = 1; i < sheet.getLastRowNum() + 1; i++) { Row row = sheet.getRow(i); JSONObject obj = new JSONObject(); // 遍历每一列 for (int j = 0; j < row.getLastCellNum(); j++) { Cell cell = row.getCell(j); String columnName = headerRow.getCell(j).getStringCellValue(); obj.put(columnName, getCellValue(cell)); } rows.put(obj); } // 输出JSON数据 System.out.println(rows.toString()); } // 获取单元格的值 private static Object getCellValue(Cell cell) { switch (cell.getCellType()) { case BLANK: return null; case BOOLEAN: return cell.getBooleanCellValue(); case ERROR: throw new RuntimeException("Cell contains an error"); case NUMERIC: return cell.getNumericCellValue(); case STRING: return cell.getStringCellValue(); case FORMULA: return getCellValue(cell.getCachedFormulaResultType() == CellType.NUMERIC ? cell : cell.getSheet().getRow(cell.getRowIndex()).getCell(cell.getColumnIndex())); default: throw new RuntimeException("Cell type not supported"); } } } ``` 请注意,此代码示例仅适用于Excel表格中的简单数据类型,如字符串、字和布尔值。对于复杂数据类型,如日期和时间,您需要编写自定义的转换逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值