java读取excel某个单元格的值_java poi怎么获取excel单元格的内容

展开全部

package edu.sjtu.erplab.poi;

import java.io.InputStream&ch=ww.xqy.chain" target="_blank" class="link-baike">FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

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;

/**

* 操作Excel表格的功能类

*/

public class ExcelReader {

private POIFSFileSystem fs;

private HSSFWorkbook wb;

private HSSFSheet sheet;

private HSSFRow row;

/**

* 读取Excel表格表头的内容

* @32313133353236313431303231363533e78988e69d8331333365643662param InputStream

* @return String 表头内容的数组

*/

public String[] readExcelTitle(InputStream is) {

try {

fs = new POIFSFileSystem(is);

wb = new HSSFWorkbook(fs);

} catch (IOException e) {

e.printStackTrace();

}

sheet = wb.getSheetAt(0);

row = sheet.getRow(0);

// 标题总列数

int colNum = row.getPhysicalNumberOfCells();

System.out.println("colNum:" + colNum);

String[] title = new String[colNum];

for (int i = 0; i 

//title[i] = getStringCellValue(row.getCell((short) i));

title[i] = getCellFormatValue(row.getCell((short) i));

}

return title;

}

/**

* 读取Excel数据内容

* @param InputStream

* @return Map 包含单元格数据内容的Map对象

*/

public Map readExcelContent(InputStream is) {

Map content = new HashMap();

String str = "";

try {

fs = new POIFSFileSystem(is);

wb = new HSSFWorkbook(fs);

} catch (IOException e) {

e.printStackTrace();

}

sheet = wb.getSheetAt(0);

// 得到总行数

int rowNum = sheet.getLastRowNum();

row = sheet.getRow(0);

int colNum = row.getPhysicalNumberOfCells();

// 正文内容应该从第二行开始,第一行为表头的标题

for (int i = 1; i <= rowNum; i++) {

row = sheet.getRow(i);

int j = 0;

while (j 

// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据

// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean

// str += getStringCellValue(row.getCell((short) j)).trim() +

// "-";

str += getCellFormatValue(row.getCell((short) j)).trim() + "    ";

j++;

}

content.put(i, str);

str = "";

}

return content;

}

/**

* 获取单元格数据内容为字符串类型的数据

*

* @param cell Excel单元格

* @return String 单元格数据内容

*/

private String getStringCellValue(HSSFCell cell) {

String strCell = "";

switch (cell.getCellType()) {

case HSSFCell.CELL_TYPE_STRING:

strCell = cell.getStringCellValue();

break;

case HSSFCell.CELL_TYPE_NUMERIC:

strCell = String.valueOf(cell.getNumericCellValue());

break;

case HSSFCell.CELL_TYPE_BOOLEAN:

strCell = String.valueOf(cell.getBooleanCellValue());

break;

case HSSFCell.CELL_TYPE_BLANK:

strCell = "";

break;

default:

strCell = "";

break;

}

if (strCell.equals("") || strCell == null) {

return "";

}

if (cell == null) {

return "";

}

return strCell;

}

/**

* 获取单元格数据内容为日期类型的数据

*

* @param cell

*            Excel单元格

* @return String 单元格数据内容

*/

private String getDateCellValue(HSSFCell cell) {

String result = "";

try {

int cellType = cell.getCellType();

if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {

Date date = cell.getDateCellValue();

result = (date.getYear() + 1900) + "-" + (date.getMonth() + 1)

+ "-" + date.getDate();

} else if (cellType == HSSFCell.CELL_TYPE_STRING) {

String date = getStringCellValue(cell);

result = date.replaceAll("[年月]", "-").replace("日", "").trim();

} else if (cellType == HSSFCell.CELL_TYPE_BLANK) {

result = "";

}

} catch (Exception e) {

System.out.println("日期格式不正确!");

e.printStackTrace();

}

return result;

}

/**

* 根据HSSFCell类型设置数据

* @param cell

* @return

*/

private String getCellFormatValue(HSSFCell cell) {

String cellvalue = "";

if (cell != null) {

// 判断当前Cell的Type

switch (cell.getCellType()) {

// 如果当前Cell的Type为NUMERIC

case HSSFCell.CELL_TYPE_NUMERIC:

case HSSFCell.CELL_TYPE_FORMULA: {

// 判断当前的cell是否为Date

if (HSSFDateUtil.isCellDateFormatted(cell)) {

// 如果是Date类型则,转化为Data格式

//方法1:这样子的data格式是带时分秒的:2011-10-12 0:00:00

//cellvalue = cell.getDateCellValue().toLocaleString();

//方法2:这样子的data格式是不带带时分秒的:2011-10-12

Date date = cell.getDateCellValue();

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

cellvalue = sdf.format(date);

}

// 如果是纯数字

else {

// 取得当前Cell的数值

cellvalue = String.valueOf(cell.getNumericCellValue());

}

break;

}

// 如果当前Cell的Type为STRIN

case HSSFCell.CELL_TYPE_STRING:

// 取得当前的Cell字符串

cellvalue = cell.getRichStringCellValue().getString();

break;

// 默认的Cell值

default:

cellvalue = " ";

}

} else {

cellvalue = "";

}

return cellvalue;

}

public static void main(String[] args) {

try {

// 对读取Excel表格标题测试

InputStream is = new FileInputStream("d:\\test2.xls");

ExcelReader excelReader = new ExcelReader();

String[] title = excelReader.readExcelTitle(is);

System.out.println("获得Excel表格的标题:");

for (String s : title) {

System.out.print(s + " ");

}

// 对读取Excel表格内容测试

InputStream is2 = new FileInputStream("d:\\test2.xls");

Map map = excelReader.readExcelContent(is2);

System.out.println("获得Excel表格的内容:");

for (int i = 1; i <= map.size(); i++) {

System.out.println(map.get(i));

}

} catch (FileNotFoundException e) {

System.out.println("未找到指定路径的文件!");

e.printStackTrace();

}

}

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279167.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

科研绘图工具软件_如何在Windows 10 Mail中使用绘图工具

科研绘图工具软件Microsoft recently released a new feature for the Windows 10 Mail app that lets you convey messages with drawings right inside the body of an email. This is a great way to quickly sketch things like graphs or tables to get your point across…

子元素相对于父元素垂直居中对齐

记个笔记 1. 元素相对于浏览器居中 <style>.window-center {/* 将position设置为fixed&#xff0c;使元素相对于浏览器窗口定位 */position: fixed;/* 将margin设置为auto&#xff0c;使浏览器自动推算元素外边距 */margin: auto;/* 将上下左右边距&#xff08;相对于浏览…

SQL Server编程(06)触发器

SQL Server 通过触发器用来保证业务逻辑和数据的完整性。在SQL Server中&#xff0c;触发器是一种特殊类型的存储过程&#xff0c;可在执行语言事件时自动触发。SQL Server中触发器包括三种&#xff1a;DML触发器、DDL触发器和登录触发器。 DML触发器&#xff1a;执行DML语句触…

网站运行java_定制化Azure站点Java运行环境(5)

Java 8下PermGen及参数设置在上一章节中&#xff0c;我们定制化使用了Java 8环境&#xff0c;使用我们的测试页面打印出了JVM基本参数&#xff0c;但如果我们自己观察&#xff0c;会发现在MXBeans中&#xff0c;没有出现PermGen的使用数据&#xff0c;初始大小等信息&#xff0…

三阶魔方魔方公式_观看此魔方的自我解决

三阶魔方魔方公式Finally: a Rubik’s cube that can solve itself. A maker named Human Controller built it in Japan, and you can see it in action right now. 最后&#xff1a;一个可以解决自身问题的魔方。 一家名为Human Controller的制造商在日本制造了它&#xff0…

pc样式在ie8中的bug

2019独角兽企业重金招聘Python工程师标准>>> pc样式在ie8中的bug 1,box-sizing:border-box: 在ie中,此属性的使用有限制: (在IE8中&#xff0c;min-width属性适用于content-box即使box-sizing设置为border-box。 Chrome select在使用时从元素中选择选项时遇到问…

下载: 虾米音乐_您所说的内容:如何组织凌乱的音乐收藏

下载: 虾米音乐Earlier this week we asked you to share your tips, tricks, and tools, for managing a messy music collection. Now we’re back to share so great reader tips; read on to find ways to tame your mountain of music. 本周早些时候&#xff0c;我们要求您…

Django form表单

Django form表单 目录 普通方式手写注册功能 views.pylogin.html使用form组件实现注册功能 views.pylogin2.html常用字段与插件 initialerror_messagespasswordradioSelect单选Select多选Select单选checkbox多选checkboxDjango Form所有内置字段校验补充进阶 应用Bootstrap样式…

java 多线程 优先级_java多线程之线程的优先级

在操作系统中&#xff0c;线程可以划分优先级&#xff0c;优先级较高的线程得到CPU资源较多&#xff0c;也就是CPU优先执行优先级较高的线程对象中的任务(其实并不是这样)。在java中&#xff0c;线程的优先级用setPriority()方法就行&#xff0c;线程的优先级分为1-10这10个等级…

PyQt5应用与实践

2015-01-16 19:00 by 吴秦, 69476 阅读, 5 评论, 收藏, 编辑 一个典型的GUI应用程序可以抽象为&#xff1a;主界面&#xff08;菜单栏、工具栏、状态栏、内容区域&#xff09;&#xff0c;二级界面&#xff08;模态、非模态&#xff09;&#xff0c;信息提示&#xff08;Toolti…

plex实现流媒体服务器_Plex继续远离服务器,提供网络节目

plex实现流媒体服务器() Plex now offers a “Web Shows” feature in certain versions of their interface, providing access to shows from brands like TWiT, GQ, and Popular Science. Plex现在在其界面的某些版本中提供了“网络节目”功能&#xff0c;可以访问TWiT&…

MIME协议(三) -- MIME邮件的组织结构

一封MIME邮件可以由多个不同类型的MIME消息组合而成&#xff0c;一个MIME消息表示邮件中的一个基本MIME资源或若干基本MIME消息的组合体。每个MIME消息的数据格式与RFC822数据格式相似&#xff0c;也包括头和体两部分&#xff0c;分别称为MIME消息头和MIME消息体&#xff0c;它…

discord linux_最好的Discord机器人来启动服务器

discord linuxDiscord has an extensive API and good support for bots on their platform. Because of this, there are tons of bots to go around. However, many of them just copy one another’s functionality. We’ve picked out the ones that do it right, and comp…

java获取前端json数据_java如何获取前端ajax传来的json对象

假设使用 jQuery 中的 ajax1. Json 对象前端代码示例$.ajax({url : http://localhost:8888/demo,type: post,data: {userName:15488779956}success: function(data) {// TODO}})后台代码示例RestControllerpublic class Demo {/*** 方法 1 使用 HttpServletRequest 接收* */Req…

版本控制介绍以及常用的版本控制工具

版本控制是指对软件开发过程中各种程序代码、配置文件及说明文档等文件变更的管理&#xff0c;是软件配置管理的核心思想之一。 编写一个成熟可用的程序是一个工作量很大的工程&#xff0c;并非我们一次性就可以搞定的工作&#xff0c;所以在开发过程当中需要&#xff1a; 1、 …

2019年4月第四周_2012年4月最佳怪胎文章

2019年4月第四周This past month we covered topics such as how to use a 64-bit web browser on Windows, the best tips and tweaks for getting the most out of Firefox, how to check out library books on your Kindle for free, and more. Join us as we look back at …

matlab循环遍历数组_Matlab - 访问for循环中最大值的索引,并使用它从数组中删除值...

我想递归地找到一系列矩阵中的最大值(第8列&#xff0c;具体)&#xff0c;然后使用该最大值的索引来设置数组中的所有值&#xff0c;索引最大为NaN的最大索引(对于列14:16) . 很容易找到最大值和索引&#xff0c;但是使用for循环为多个数组做这件事我很难过 .如果没有for循环&a…

【资料整理】编译安装nginx

【nginx】编译安装nginx 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311…

游荡的奶牛

沙雕题目 读错题了&#xff0c;不想多说 转载于:https://www.cnblogs.com/bullshit/p/9811058.html

物体成瘾性_科技成瘾使我们不那么快乐。 那是一个市场机会。

物体成瘾性Compulsively checking social networks makes us less happy. I think we all understand this intuitively, the same way we understand that working out more and eating better is a good idea. 强迫检查社交网络使我们不那么开心。 我认为我们所有人都可以凭直…