「java传递表格」java 读取表格

博主:adminadmin 2022-11-29 13:10:08 61

今天给各位分享java传递表格的知识,其中也会对java 读取表格进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA表格传值问题,求解答。

html代码:

table width=100% border=0 id="detailTable"

tr

td width="4%"

input type="checkbox" onclick="checkAll(this)"

/td

td width="8%"

订单行号

/td

td width="11%"

工具代码

/td

td width="8%"

订货数量

/td

td width="8%"

单位编码

/td

td width="11%"

需求日期

/td

td width="8%"

默认子库

/td

td

不含税价

/td

td

实际价格

/td

td

计划价格

/td

td

备注

/td

/tr

tr

td

input type="checkbox" name="check" id="0"

/td

td

input name="podetails[0].polinenumber" style="width: 100%"

id="00" value="00001" readonly="readonly" /

/td

td

input name="podetails[0].toolcode" style="width: 100%"

id="01" oncontextmenu="return false" onmousedown="rightclick(event,this)" /

/td

td

input name="podetails[0].qtyordered" style="width: 100%"

id="02" /

/td

td

input name="podetails[0].unitcode" style="width: 100%"

id="03" oncontextmenu="return false" onmousedown="rightclick(event,this)" /

/td

td

input name="podetails[0].requiredate" style="width: 100%"

id="04" onclick="return showCalendar(this, '%Y/%m/%d');" /

/td

td

input name="podetails[0].subinventory" style="width: 100%"

id="05" /

/td

td

input name="podetails[0].beforetax" style="width: 100%"

id="05" /

/td

td

input name="podetails[0].saleprice" style="width: 100%"

id="07" /

/td

td

input name="podetails[0].unitprice" style="width: 100%"

id="08" /

/td

td

input name="podetails[0].notes" style="width: 90%" id="09" /

/td

/tr

/table

table width="100%" border="0"

tr

td

/td

th colspan="7" align="left"

input type="button" value="添加行" onclick="insertRow()"

input type="button" value="删除行" onclick="deleteRow()"

/th

td

/td

/tr

/table

JS脚本:

//插入表格行

function insertRow()

{

var checks = document.getElementsByName("check");

var t = checks.length;

var i = parseInt(checks[parseInt(t-1)].id, 10) + 1;

var polinenumber;

if (t == 0) {

polinenumber = "00001";

} else {

var prenumber = document.getElementById((i-1).toString() + "0").value;

polinenumber =("00000" + (parseInt(prenumber, 10) + 1).toString()).slice(-5);

}

var detailTable = document.getElementById("detailTable");

var newRow = detailTable.insertRow();

var cell1 = newRow.insertCell();

cell1.innerHTML = 'input type="checkbox" name="check" id="' + i + '"';

var cell2 = newRow.insertCell();

cell2.innerHTML = 'input name="podetails[' + i + '].polinenumber" style="width:100%" id="' + i + '0" value="' + polinenumber + '" readonly="readonly" /';

var cell3 = newRow.insertCell();

cell3.innerHTML = 'input name="podetails[' + i + '].toolcode" style="width:100%" id="' + i + '1" oncontextmenu="return false" onmousedown="rightclick(event,this)" /';

var cell4 = newRow.insertCell();

cell4.innerHTML = 'input name="podetails[' + i + '].qtyordered" style="width:100%" id="' + i + '2" /';

var cell5 = newRow.insertCell();

cell5.innerHTML = 'input name="podetails[' + i + '].unitcode" style="width:100%" id="' + i + '3" oncontextmenu="return false" onmousedown="rightclick(event,this)" /';

var cell6 = newRow.insertCell();

cell6.innerHTML = 'input name="podetails[' + i + '].requiredate" style="width:100%" id="' + i + '4" onclick="return showCalendar(this, "%Y/%m/%d");" /';

var cell7 = newRow.insertCell();

cell7.innerHTML = 'input name="podetails[' + i + '].subinventory" style="width:100%" id="' + i + '5" /';

var cell8 = newRow.insertCell();

cell8.innerHTML = 'input name="podetails[' + i + '].beforetax" style="width:100%" id="' + i + '6" /';

var cell9 = newRow.insertCell();

cell9.innerHTML = 'input name="podetails[' + i + '].saleprice" style="width:100%" id="' + i + '7" /';

var cell8 = newRow.insertCell();

cell8.innerHTML = 'input name="podetails[' + i + '].unitprice" style="width:100%" id="' + i + '8" /';

var cell9 = newRow.insertCell();

cell9.innerHTML = 'input name="podetails[' + i + '].notes" style="width:90%" id="' + i + '9" /';

}

//删除表格行

function deleteRow() {

var detailTable = document.getElementById("detailTable");

var checks = document.getElementsByName("check");

for (var i = checks.length - 1; i = 0; i--) {

if (checks[i].checked) {

var index = checks[i].parentNode.parentNode.rowIndex;

if (checks.length 1) {

detailTable.deleteRow(index);

} else {

alert("\u6700\u540e\u4e00\u884c\u4e0d\u80fd\u5220\u9664\uff01\uff01\uff01");

}

}

}

if (checks.length parseInt(checks[parseInt(checks.length - 1, 10)].id, 10) + 1) {

for (var i = 1; i = checks.length; i++) {

document.getElementById(checks[i-1].id + "0").value = ("00000" + i.toString()).slice(-5);

}

}

}

//重置索引

function resetIndex()

{

var inputs = document.getElementsByTagName("input");

for(var i=0;iinputs.length;i++)

{

if(inputs[i].name.indexOf('podetails[')!=-1)

{

var index =inputs[i].parentNode.parentNode.rowIndex;

inputs[i].name = inputs[i].name.replace(/\d+/,index-1);

}

}

}

//全选

function checkAll(obj)

{

var checks = document.getElementsByName("check");

for(var i=checks.length-1;i=0;i--)

{

checks[i].checked = obj.checked;

}

}

后台Java代码:

Form代码:

/*

* Generated by MyEclipse Struts

* Template path: templates/java/JavaClass.vtl

*/

package com.smtcl.business.struts.form;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

import com.muhe.tools.AutoArrayList;

import com.smtcl.business.beans.Podetail;

import com.smtcl.business.beans.Po;

/**

* MyEclipse Struts

* Creation date: 09-27-2010

*

* XDoclet definition:

* @struts.form name="poForm"

*/

@SuppressWarnings("unchecked")

public class PoForm extends ActionForm {

/**

*

*/

private static final long serialVersionUID = 1L;

/*

* Generated fields

*/

/** po property */

private Po po = new Po();

/** pod property */

private ListPodetail podetails = new AutoArrayList(Podetail.class);

/*

* Generated Methods

*/

/**

* Method validate

* @param mapping

* @param request

* @return ActionErrors

*/

public ActionErrors validate(ActionMapping mapping,

HttpServletRequest request) {

// TODO Auto-generated method stub

return null;

}

/**

* Method reset

* @param mapping

* @param request

*/

public void reset(ActionMapping mapping, HttpServletRequest request) {

// TODO Auto-generated method stub

}

public Po getPo() {

return po;

}

public void setPo(Po po) {

this.po = po;

}

public ListPodetail getPodetails() {

return podetails;

}

public void setPodetails(ListPodetail podetails) {

this.podetails.clear();

this.podetails = podetails;

}

}

/** po property */

private Po po = new Po();

/** pod property */

private ListPodetail podetails = new AutoArrayList(Podetail.class);

/*

* Generated Methods

*/

处理动态提交的数据,具体方法根据业务需求自动手做。

public class PoAddAction extends Action {

/*

* Generated Methods

*/

/**

* Method execute

* @param mapping

* @param form

* @param request

* @param response

* @return ActionForward

*/

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

PoForm poForm = (PoForm) form;// TODO Auto-generated method stub

com.smtcl.business.beans.Po po = poForm.getPo();

Listcom.smtcl.business.beans.Podetail podetails = poForm.getPodetails();

System.out.println(po.getStorerkey());

System.out.println("------");

System.out.println(podetails.size());

return null;

}

}

Java上传Excel表格?

public boolean myapp_readexcel(String filePath, int sheetNum) {//上传的文件地址,sheetNum这个是excel下的那个sheet1的标志,sheet1这里对应数字是0

InputStream ins = null;

Workbook book = null;

try {

ins = new FileInputStream(filePath);

book = WorkbookFactory.create(ins);

getInsertData(book.getSheetAt(sheetNum));//这里面进行读取操作和业务逻辑

ins.close();

} catch (Exception e1) {

e1.printStackTrace();

} finally {

if (ins != null) {

try {

ins.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return res;

}

private boolean getInsertData(Sheet sheet) {

boolean res = false;

IteratorRow rit = sheet.iterator();//每一行的迭代

while (rit.hasNext()) {

lineMap = getDataByRow(rit);

System.out.println(lineMap.get("0"));

System.out.println(lineMap.get("1"));...................................

}

}

public MapString,String getDataByRow(IteratorRow rit){

MapString, String lineMap;

Row row;

Cell cell;

row = rit.next();

lineMap = new ConcurrentHashMapString, String(); // 用于接收每列的数据。

for (int i = 0; i = row.getLastCellNum(); i++) {

cell = row.getCell(i);

String k = ""; // 用于接收每个单元格的数据。

if (cell == null) {

lineMap.put(String.valueOf((i)), k); // 赋值。

continue;

}

switch (cell.getCellType()) {

case Cell.CELL_TYPE_BLANK:

k = "";

break;

case Cell.CELL_TYPE_ERROR:

k = Byte.toString(cell.getErrorCellValue());

break;

case Cell.CELL_TYPE_STRING:

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

break;

case Cell.CELL_TYPE_NUMERIC:

if (DateUtil.isCellDateFormatted(cell)) {

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

k = sdf.format(cell.getDateCellValue());

} else {

k = Integer.toString((int) cell.getNumericCellValue());

}

break;

case Cell.CELL_TYPE_BOOLEAN:

k = Boolean.toString(cell.getBooleanCellValue());

break;

case Cell.CELL_TYPE_FORMULA:

k = cell.getCellFormula();

break;

default:

k = "";

}

if ((k != null) (!"".equals(k))) {

lineMap.put(String.valueOf((i)), k); // 赋值。

} else {

lineMap.put(String.valueOf((i)), ""); // 赋值。

}

}

return lineMap;

}

java中A表格与B表格之间数据传输

用的是JTable吗?

如果是就很简单,JTableB.setModel(JTableA.getModel());

数据就复制过去了

java后台生成了一个表格,用流传到前台,请问怎么接收呀?在线等

,这样单纯的拷贝文件是不可以的,必须要用到上传的组件,常用的上传组件:Apache 的 Commons FileUploadJavaZoom的UploadBeanjspSmartUpload网上这方面的资料太多了,FileUpload是较经常使用的,大致上传代码是pre t="code" l="java"DiskFileItemFactory factory = new DiskFileItemFactory();

ServletFileUpload upload = new ServletFileUpload(factory);

try {

List items = upload.parseRequest(request);

Iterator itr = items.iterator();

while (itr.hasNext()) {

FileItem item = (FileItem) itr.next();

if (item.isFormField()) {

System.out.println("表单参数名:" + item.getFieldName() + ",表单参数值:" + item.getString("UTF-8"));

} else {

if (item.getName() != null !item.getName().equals("")) {

System.out.println("上传文件的大小:" + item.getSize());

System.out.println("上传文件的类型:" + item.getContentType());

// item.getName()返回上传文件在客户端的完整路径名称

System.out.println("上传文件的名称:" + item.getName());

File tempFile = new File(item.getName());

//上传文件的保存路径

File file = new File(sc.getRealPath("/") + savePath, tempFile.getName());

item.write(file);

request.setAttribute("upload.message", "上传文件成功!");

}else{

request.setAttribute("upload.message", "没有选择上传文件!");

}

}

}

}catch(FileUploadException e){

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

request.setAttribute("upload.message", "上传文件失败!");

}希望可以帮助到你

怎么把数据库表格导入到java上

public static void main(String args[]) throws BiffException, IOException, WriteException{

//1 从Excel文件读取数据表

//Java Excel API既可以从本地文件系统的一个文件(.xls),也可以从输入流中读取Excel数据表。

//读取Excel数据表的第一步是创建Workbook(术语:工作薄),下面的代码片段举例说明了应该如何操作:

//(完整代码见ExcelReading.java)

try

{

//构建Workbook对象, 只读Workbook对象

//直接从本地文件创建Workbook

//从输入流创建Workbook

InputStream is = new FileInputStream("D:/user.xls");

jxl.Workbook rwb = Workbook.getWorkbook(is);

//一旦创建了Workbook,我们就可以通过它来访问Excel Sheet(术语:工作表)。参考下面的代码片段:

//获取第一张Sheet表

Sheet rs = (Sheet) rwb.getSheet(0);

//我们既可能通过Sheet的名称来访问它,也可以通过下标来访问它。如果通过下标来访问的话,

//要注意的一点是下标从0开始,就像数组一样。

//一旦得到了Sheet,我们就可以通过它来访问Excel Cell(术语:单元格)。参考下面的代码片段:

//获取第一行,第一列的值

Cell c00 = ((jxl.Sheet) rs).getCell(0, 0);

String strc00 = c00.getContents();

//获取第一行,第二列的值

Cell c10 = ((jxl.Sheet) rs).getCell(1, 0);

String strc10 = c10.getContents();

//获取第二行,第二列的值

Cell c11 = ((jxl.Sheet) rs).getCell(1, 1);

String strc11 = c11.getContents();

System.out.println("Cell(0, 0)" + " value : " + strc00 + "; type : " + c00.getType());

System.out.println("Cell(1, 0)" + " value : " + strc10 + "; type : " + c10.getType());

System.out.println("Cell(1, 1)" + " value : " + strc11 + "; type : " + c11.getType());

//如果仅仅是取得Cell的值,我们可以方便地通过getContents()方法,

//它可以将任何类型的Cell值都作为一个字符串返回。示例代码中Cell(0, 0)是文本型,

//Cell(1, 0)是数字型,Cell(1,1)是日期型,通过getContents(),三种类型的返回值都是字符型。

//如果有需要知道Cell内容的确切类型,API也提供了一系列的方法。参考下面的代码片段:

String strcc00 = null;

double strcc10 = 0.00;

Date strcc11 = null;

Cell cc00 = ((jxl.Sheet) rs).getCell(0, 0);

Cell cc10 = ((jxl.Sheet) rs).getCell(1, 0);

Cell cc11 = ((jxl.Sheet) rs).getCell(1, 1);

if(c00.getType() == CellType.LABEL)

{

LabelCell labelc00 = (LabelCell)cc00;

strcc00 = labelc00.getString();

}

if(c10.getType() == CellType.NUMBER)

{

NumberCell numc10 = (NumberCell)cc10;

strcc10 = numc10.getValue();

}

if(c11.getType() == CellType.DATE)

{

DateCell datec11 = (DateCell)cc11;

strcc11 = datec11.getDate();

}

System.out.println("Cell(0, 0)" + " value : " + strcc00 + "; type : " + cc00.getType());

System.out.println("Cell(1, 0)" + " value : " + strcc10 + "; type : " + cc10.getType());

System.out.println("Cell(1, 1)" + " value : " + strcc11 + "; type : " + cc11.getType());

//在得到Cell对象后,通过getType()方法可以获得该单元格的类型,然后与API提供的基本类型相匹配,

//强制转换成相应的类型,最后调用相应的取值方法getXXX(),就可以得到确定类型的值。

//API提供了以下基本类型,与Excel的数据格式相对应,如下图所示:

//每种类型的具体意义,请参见Java Excel API Document。

//当你完成对Excel电子表格数据的处理后,一定要使用close()方法来关闭先前创建的对象,

//以释放读取数据表的过程中所占用的内存空间,在读取大量数据时显得尤为重要。参考如下代码片段:

//操作完成时,关闭对象,释放占用的内存空间

rwb.close();

}

catch (Exception e)

{

e.printStackTrace();

}

//Java Excel API提供了许多访问Excel数据表的方法,在这里我只简要地介绍几个常用的方法,

//其它的方法请参考附录中的Java Excel API Document。

//Workbook类提供的方法

//1. int getNumberOfSheets();

//获得工作薄(Workbook)中工作表(Sheet)的个数,示例:

jxl.Workbook rwb = jxl.Workbook.getWorkbook(new File("D:/user.xls"));

int sheets = rwb.getNumberOfSheets();

//2. Sheet[] getSheets();

//返回工作薄(Workbook)中工作表(Sheet)对象数组,示例:

jxl.Workbook rwb2 = jxl.Workbook.getWorkbook(new File("D:/user.xls"));

Sheet[] sheets2 = (Sheet[]) rwb2.getSheets();

//3. String getVersion();

//返回正在使用的API的版本号,好像是没什么太大的作用。

jxl.Workbook rwb3 = jxl.Workbook.getWorkbook(new File("D:/user.xls"));

String apiVersion = rwb3.getVersion();

//Sheet接口提供的方法

//1) String getName();

//获取Sheet的名称,示例:

jxl.Workbook rwb4 = jxl.Workbook.getWorkbook(new File("D:/user.xls"));

jxl.Sheet rs = rwb4.getSheet(0);

String sheetName = rs.getName();

//2) int getColumns()

//获取Sheet表中所包含的总列数,示例:

jxl.Workbook rwb5 = jxl.Workbook.getWorkbook(new File("D:/user.xls"));

jxl.Sheet rs2 = rwb5.getSheet(0);

int rsColumns = rs2.getColumns();

//3) Cell[] getColumn(int column)

//获取某一列的所有单元格,返回的是单元格对象数组,示例:

jxl.Workbook rwb6 = jxl.Workbook.getWorkbook(new File("D:/user.xls"));

jxl.Sheet rs3 = rwb6.getSheet(0);

Cell[] cell = rs3.getColumn(0);

//4) int getRows()

//获取Sheet表中所包含的总行数,示例:

jxl.Workbook rwb7 = jxl.Workbook.getWorkbook(new File("D:/user.xls"));

jxl.Sheet rs4 = rwb7.getSheet(0);

int rsRows = rs4.getRows();

//5) Cell[] getRow(int row)

//获取某一行的所有单元格,返回的是单元格对象数组,示例子:

jxl.Workbook rwb8 = jxl.Workbook.getWorkbook(new File("D:/user.xls"));

jxl.Sheet rs5 = rwb8.getSheet(0);

Cell[] cell5 = rs5.getRow(0);

//6) Cell getCell(int column, int row)

//获取指定单元格的对象引用,需要注意的是它的两个参数,第一个是列数,第二个是行数,

//这与通常的行、列组合有些不同。

jxl.Workbook rwb9 = jxl.Workbook.getWorkbook(new File("D:/user.xls"));

jxl.Sheet rs6 = rwb9.getSheet(0);

Cell cell6 = rs6.getCell(0, 0);

关于java传递表格和java 读取表格的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-29,除非注明,否则均为首码项目网原创文章,转载请注明出处。