用javaPOI包的方法获取excel中的行列号 20
现在需要用java的POI包的方法获取到excel某一个单元格的坐标,也就是他的行号和列号,比如:ABCD1afvs2sedm我现在确定了f的位置,用什么方法可以得到他的...
现在需要用java的POI包的方法获取到excel某一个单元格的坐标,也就是他的行号 和 列号 ,比如:
A B C D
1 a f v s
2 s e d m
我现在确定了 f的位置,用什么方法可以得到他的列号,也就是如何得到“B”。poi里面提供这种方法了吗? 展开
A B C D
1 a f v s
2 s e d m
我现在确定了 f的位置,用什么方法可以得到他的列号,也就是如何得到“B”。poi里面提供这种方法了吗? 展开
3个回答
展开全部
XlsMain .java 类
//该类有main方法,主要负责运行程序,同时该类中也包含了用poi读取Excel(2003版)
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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;
/**
*
* @author Hongten</br>
*
* 参考地址:http://hao0610.iteye.com/blog/1160678
*
*/
public class XlsMain {
public static void main(String[] args) throws IOException {
XlsMain xlsMain = new XlsMain();
XlsDto xls = null;
List<XlsDto> list = xlsMain.readXls();
try {
XlsDto2Excel.xlsDto2Excel(list);
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < list.size(); i++) {
xls = (XlsDto) list.get(i);
System.out.println(xls.getXh() + " " + xls.getXm() + " "
+ xls.getYxsmc() + " " + xls.getKcm() + " "
+ xls.getCj());
}
}
/**
* 读取xls文件内容
*
* @return List<XlsDto>对象
* @throws IOException
* 输入/输出(i/o)异常
*/
private List<XlsDto> readXls() throws IOException {
InputStream is = new FileInputStream("pldrxkxxmb.xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
XlsDto xlsDto = null;
List<XlsDto> list = new ArrayList<XlsDto>();
// 循环工作表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;
}
/**
* 得到Excel表中的值
*
* @param hssfCell
* Excel中的每一个格子
* @return Excel中每一个格子中的值
*/
@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
return String.valueOf(hssfCell.getNumericCellValue());
} else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue());
}
}
}
//该类有main方法,主要负责运行程序,同时该类中也包含了用poi读取Excel(2003版)
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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;
/**
*
* @author Hongten</br>
*
* 参考地址:http://hao0610.iteye.com/blog/1160678
*
*/
public class XlsMain {
public static void main(String[] args) throws IOException {
XlsMain xlsMain = new XlsMain();
XlsDto xls = null;
List<XlsDto> list = xlsMain.readXls();
try {
XlsDto2Excel.xlsDto2Excel(list);
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < list.size(); i++) {
xls = (XlsDto) list.get(i);
System.out.println(xls.getXh() + " " + xls.getXm() + " "
+ xls.getYxsmc() + " " + xls.getKcm() + " "
+ xls.getCj());
}
}
/**
* 读取xls文件内容
*
* @return List<XlsDto>对象
* @throws IOException
* 输入/输出(i/o)异常
*/
private List<XlsDto> readXls() throws IOException {
InputStream is = new FileInputStream("pldrxkxxmb.xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
XlsDto xlsDto = null;
List<XlsDto> list = new ArrayList<XlsDto>();
// 循环工作表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;
}
/**
* 得到Excel表中的值
*
* @param hssfCell
* Excel中的每一个格子
* @return Excel中每一个格子中的值
*/
@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
return String.valueOf(hssfCell.getNumericCellValue());
} else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue());
}
}
}
展开全部
XlsMain
.java
类
//该类有main方法,主要负责运行程序,同时该类中也包含了用poi读取Excel(2003版)
import
java.io.FileInputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.util.ArrayList;
import
java.util.List;
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;
/**
*
*
@author
Hongten</br>
*
*
参考地址:http://hao0610.iteye.com/blog/1160678
*
*/
public
class
XlsMain
{
public
static
void
main(String[]
args)
throws
IOException
{
XlsMain
xlsMain
=
new
XlsMain();
XlsDto
xls
=
null;
List<XlsDto>
list
=
xlsMain.readXls();
try
{
XlsDto2Excel.xlsDto2Excel(list);
}
catch
(Exception
e)
{
e.printStackTrace();
}
for
(int
i
=
0;
i
<
list.size();
i++)
{
xls
=
(XlsDto)
list.get(i);
System.out.println(xls.getXh()
+
"
"
+
xls.getXm()
+
"
"
+
xls.getYxsmc()
+
"
"
+
xls.getKcm()
+
"
"
+
xls.getCj());
}
}
/**
*
读取xls文件内容
*
*
@return
List<XlsDto>对象
*
@throws
IOException
*
输入/输出(i/o)异常
*/
private
List<XlsDto>
readXls()
throws
IOException
{
InputStream
is
=
new
FileInputStream("pldrxkxxmb.xls");
HSSFWorkbook
hssfWorkbook
=
new
HSSFWorkbook(is);
XlsDto
xlsDto
=
null;
List<XlsDto>
list
=
new
ArrayList<XlsDto>();
//
循环工作表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;
}
/**
*
得到Excel表中的值
*
*
@param
hssfCell
*
Excel中的每一个格子
*
@return
Excel中每一个格子中的值
*/
@SuppressWarnings("static-access")
private
String
getValue(HSSFCell
hssfCell)
{
if
(hssfCell.getCellType()
==
hssfCell.CELL_TYPE_BOOLEAN)
{
//
返回布尔类型的值
return
String.valueOf(hssfCell.getBooleanCellValue());
}
else
if
(hssfCell.getCellType()
==
hssfCell.CELL_TYPE_NUMERIC)
{
//
返回数值类型的值
return
String.valueOf(hssfCell.getNumericCellValue());
}
else
{
//
返回字符串类型的值
return
String.valueOf(hssfCell.getStringCellValue());
}
}
}
.java
类
//该类有main方法,主要负责运行程序,同时该类中也包含了用poi读取Excel(2003版)
import
java.io.FileInputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.util.ArrayList;
import
java.util.List;
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;
/**
*
*
@author
Hongten</br>
*
*
参考地址:http://hao0610.iteye.com/blog/1160678
*
*/
public
class
XlsMain
{
public
static
void
main(String[]
args)
throws
IOException
{
XlsMain
xlsMain
=
new
XlsMain();
XlsDto
xls
=
null;
List<XlsDto>
list
=
xlsMain.readXls();
try
{
XlsDto2Excel.xlsDto2Excel(list);
}
catch
(Exception
e)
{
e.printStackTrace();
}
for
(int
i
=
0;
i
<
list.size();
i++)
{
xls
=
(XlsDto)
list.get(i);
System.out.println(xls.getXh()
+
"
"
+
xls.getXm()
+
"
"
+
xls.getYxsmc()
+
"
"
+
xls.getKcm()
+
"
"
+
xls.getCj());
}
}
/**
*
读取xls文件内容
*
*
@return
List<XlsDto>对象
*
@throws
IOException
*
输入/输出(i/o)异常
*/
private
List<XlsDto>
readXls()
throws
IOException
{
InputStream
is
=
new
FileInputStream("pldrxkxxmb.xls");
HSSFWorkbook
hssfWorkbook
=
new
HSSFWorkbook(is);
XlsDto
xlsDto
=
null;
List<XlsDto>
list
=
new
ArrayList<XlsDto>();
//
循环工作表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;
}
/**
*
得到Excel表中的值
*
*
@param
hssfCell
*
Excel中的每一个格子
*
@return
Excel中每一个格子中的值
*/
@SuppressWarnings("static-access")
private
String
getValue(HSSFCell
hssfCell)
{
if
(hssfCell.getCellType()
==
hssfCell.CELL_TYPE_BOOLEAN)
{
//
返回布尔类型的值
return
String.valueOf(hssfCell.getBooleanCellValue());
}
else
if
(hssfCell.getCellType()
==
hssfCell.CELL_TYPE_NUMERIC)
{
//
返回数值类型的值
return
String.valueOf(hssfCell.getNumericCellValue());
}
else
{
//
返回字符串类型的值
return
String.valueOf(hssfCell.getStringCellValue());
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream("d:\\test.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
//f列索引位置
int index = 1;
HSSFCell cell = row.getCell((short) index);
String msg = cell.getStringCellValue();
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
//f列索引位置
int index = 1;
HSSFCell cell = row.getCell((short) index);
String msg = cell.getStringCellValue();
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询