我想用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对象
ListStcCoreElements 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 ListStcCoreElements getAssessCateRange(Sheet sheet, String startName, String endName, int rowNum, int titleRowNum)
{
int[] cateRange = new int[2];
ListStcCoreElements dataList = new ArrayListStcCoreElements();
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表格代码怎么写
java表格就是java swing。
//创建表头
String[] columnNames = { "First Name", "Last Name", "Sport",
"# of Years", "Vegetarian" };
//创建显示数据
Object[][] data = {
{ "Kathy", "Smith", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
{ "Sue", "Black", "Knitting", new Integer(2),
new Boolean(false) },
{ "Jane", "White", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
/*
* JTable还提供了一个重载的构造方法,传入两个Vector
* JTable(Vector rowData, Vector columnNames)
*
*/
final JTable table = new JTable(data, columnNames);
table.setBackground(Color.YELLOW);
用Java编写程序读入附件Excel文件中的表格数据,然后用命令行方式输出所有数据
/**
*
*只对你的Excel 有效,其他不能保证
* 示例代码,不能直接用到生产环境
/
package readExcel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
public class ReadXLSTools {
ListDataModel result = new ArrayListDataModel();
Row firstRow;
Row secondRow;
/**
*
* @param path
* --tell us where is the xls file
*
* please don't pass a folder path.
* @throws FileNotFoundException
*/
public void read(String path) throws FileNotFoundException {
File xlsFile = new File(path);
if (!xlsFile.exists()) {
throw new IllegalArgumentException(
"The source file cannot be found.");
}
if (xlsFile.isDirectory()) {
throw new IllegalArgumentException("Source file can't be a folder!");
}
openXLS(xlsFile);
}
private void openXLS(File xlsFile) throws FileNotFoundException {
InputStream is = new FileInputStream(xlsFile);
try {
HSSFWorkbook xlsWorkBooks = new HSSFWorkbook(is);
HSSFSheet firstSheet = xlsWorkBooks.getSheetAt(0);
for (Row row : firstSheet) {
if (row.getRowNum() == 0) {
firstRow = row;
continue;
}
if (row.getRowNum() == 1) {
secondRow = row;
continue;
}
for (Cell cell : row) {
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
Double value = (Double) getCellValue(cell);
if (value == 0.0) {
continue;
}
DataModel data = new DataModel();
data.project = String.valueOf(getCellValue(row
.getCell(0)));
int organIndex = cell.getColumnIndex();
Cell organCell = firstRow.getCell(organIndex);
String organ = String.valueOf(getCellValue(organCell));
if ("".equals(organ)) {
organ = String.valueOf(getCellValue(firstRow
.getCell(organIndex - 1)));
}
data.organ = organ;
String item = String.valueOf(getCellValue(secondRow
.getCell(organIndex)));
data.item = item;
data.value = value;
result.add(data);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private Object getCellValue(Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
return cell.getRichStringCellValue().getString();
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue();
} else {
return cell.getNumericCellValue();
}
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue();
case Cell.CELL_TYPE_FORMULA:
return cell.getCellFormula();
default:
return "";
}
}
public static void main(String[] args) {
try {
ReadXLSTools tool = new ReadXLSTools();
tool.read("d:\\test\\test.xls");
for (int i = 0; i tool.result.size(); i++) {
System.out.println(tool.result.get(i));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
package readExcel;
public class DataModel {
String organ;
String project;
String item;
double value;
@Override
public String toString(){
return "["+project+","+organ+"/"+item+"]"+value;
}
}
Java如何做动态表格?
一、动态加载表格
1.首先在html中为表格的添加位置设置id
即是在html的body标签内部写一个div标签表明表格要添加到此div的内部。如下
div id="tdl"div
2.在javascript中写添加表格的语句
若在当前html文件中,则写在script标签内部,如
代码如下:
script type="text/javascript"
document.getElementById("tbl").innerHTML="tabletrtd/td/tr/table" //此处添加的表格可根据自己需要创建
/script
若是通过引入js文件,则在js文件(假设是test.js)中直接写如下语句
代码如下:
document.getElementById("tbl").innerHTML="tabletrtd/td/tr/table"
然后再引入自己的html文件
代码如下:
script type="text/javascript" src="test.js"/script
二、 动态添加表格行
1.首先在html中为表格行的添加位置设置id,此位置必须是tbody内部(不是特别准确,但根据我的测试就得到此结论,有其他的方法请留言,谢谢),如下
代码如下:
table
thead/thead
tfoottfoot //tfoot与thead是与tbody配套使用,但我在写的时候,没用也可以。
tbody id="rows"/tbody
/table
[\s\S ]*\n
2.在javascript内容中,要先创建行和单元格,再在.tbody中添加行,如下
[code]
row=document.createElement("tr"); //创建行
td1=document.createElement("tr"); //创建单元格
td1.appendChild(document.createTextNode("content")); //为单元格添加内容
row.appendChild(td1); //将单元格添加到行内
document.getElementById("rows").append(row); //将行添加到tbody中
本文题目:java表格行列代码 表格行列数
URL标题:http://lswzjz.com/article/dososhc.html