package chexianchaxun;
import java.io.*;
public class FeiYong{
public static void main(String[] args) throws Exception {
java.net.URL url = new java.net.URL("http://********");
java.net.URLConnection con = url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
//字节流:InputStream(读取)、OutputStream(写入)
String Msg = "<Request> <VinNo>ZAMJK39E6B0060491</VinNo><LicenseNo></LicenseNo><LoginID>215288</LoginID></Request>" ;
/**转保车辆VinNo为必填
*/
System.out.println(Msg);
//字符流:Writer(写入)、Reader(读取)
dataOut.write(Msg.getBytes("gbk"));//将已经解析出来的字节数据转化为gbk编码格式的字符串
dataOut.flush();
dataOut.close();
InputStream is = con.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "gbk");
BufferedReader br = new BufferedReader(isr);
StringBuffer sb = new StringBuffer();
String templine=null;
while ((templine = br.readLine()) != null)
sb.append(templine);
System.out.println(sb.toString());
}
}
如上。
当个输入信息车架号,通过接口,可以获取对应的数据信息。
现需要实现,excl表格形式上传,表格内有多条车辆的车架号数据,通过接口,然后再获取对应的数据信息。
感谢Always_MyLoverX 的耐心回答
443193862@qq.com,楼主把你的代码、部署环境描述发过来,给你贴代码估计你系统也跑不起来,我可以给你做个简单上传demo,你自己根据业务再扩展
Java 也有NPOI 可以用
java有专门针对excel操作的类及函数。可以找一下。话说我们以后不会是同事吧?
1、确认模板 。 2、用org.apache.poi实现表格生成上传。
InputStream inp = new FileInputStream("F://downloads/wordbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
System.out.println(cell.getNumericCellValue());
function upload${prefix_file}() {
var book = document.getElementById('${prefix_file}file-restBook-1');
var files = book.files;
var file = files[0];
if(!file) {
$.messager.alert("提示", "请选择上传文件!", "info");
return;
}
var name = file.name
;
if(name.indexOf(".") == -1) {
$.messager.alert("提示", "请按照模板上传 Excel 文件!", "info");
return;
}
var suffix = name.substring(name.indexOf(".") + 1);
if( suffix != "xls" && suffix != "xlsx") {
$.messager.alert("提示", "请按照模板上传 Excel 文件!", "info");
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var data = e.target.result;
//var wb = XLSX.read(data, {type: 'binary'});
var arr = String.fromCharCode.apply(null, new Uint8Array(data));
var wb = XLSX.read(btoa(arr), {type: 'base64'});
var sheet = wb.Sheets[wb.SheetNames[0]];
var jsono = XLSX.utils.sheet_to_row_object_array(sheet);
var jsons = JSON.stringify(jsono);
$.ajax({
url : "${ctx}/book/parseExcel.shtml",
method : "post",
dataType : "json",
data : {
"data" : jsons,
},
success : function(data) {
var result = eval(data);
$.messager.alert("提示", result.msg, "info", function() {
parent.closeUploadWin${win_id}();
});
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
$.messager.alert("提示", "页面出现异常,请联系管理员!", "error");
}
});
};
//reader.readAsBinaryString(file);
reader.readAsArrayBuffer(file);
}
fileChooser = new JFileChooser();
//过滤Excel文件,只寻找以xls结尾的Excel文件,如果想过滤word文档也可以写上doc
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "xls");
fileChooser.setFileFilter(filter);
int returnValue = fileChooser.showOpenDialog(null);
//弹出一个文件选择提示框
if (returnValue == fileChooser.APPROVE_OPTION) {
//当用户选择文件后获取文件路径
chooseFile = fileChooser.getSelectedFile();
//根据文件路径初始化Excel工作簿
Workbook workBook=null;
try {
workBook = Workbook.getWorkbook(chooseFile);
} catch (Exception be) {
be.printStackTrace();
}
//获取该工作表中的第一个工作表
Sheet sheet=workBook.getSheet(0);
//获取该工作表的行数,以供下面循环使用
int rowSize=sheet.getRows();
//获取界面数据
TransInVO aggvo = (TransInVO) getModel().getSelectedData();
List bvoList = new ArrayList();
for(int i = 1 ; i < rowSize ; i++){
//获取第A列第i行,注意Excel中的行和列都是从0开始获取的,A列为0列
if(i == 1){
String material = sheet.getCell(0,0).getContents();
material = material.replaceAll(" ", "");
if(!material.equals("物料编码"))
ExceptionUtils.wrappBusinessException("Excel第一行第A列必须是物料编码!");
String batchcode = sheet.getCell(1,0).getContents();
batchcode = batchcode.replaceAll(" ", "");
if(!batchcode.equals("批次编号"))
ExceptionUtils.wrappBusinessException("Excel第一行第B列必须是批次编号!");
String num = sheet.getCell(2,0).getContents();
num = num.replaceAll(" ", "");
if(!num.equals("数量"))
ExceptionUtils.wrappBusinessException("Excel第一行第C列必须是数量!");
}
String value1 = sheet.getCell(0,i).getContents();
String value2 = sheet.getCell(1,i).getContents();
String value3 = sheet.getCell(2,i).getContents();
if((value1 == null || value1.isEmpty()) && (value2 == null || value2.isEmpty()) && (value3 == null || value3.isEmpty())){
JOptionPane.showMessageDialog(null, "导入完成,行数 " + (i-1), "提示" , JOptionPane.PLAIN_MESSAGE);
break;
}
TransInBodyVO bvo = new TransInBodyVO();
bvo.setCrowno(i*10+"");
String materialoid = (String) LinkMidDataUtil.getHypubbo().findColValue("bd_material", "pk_material", " code = '"+value1+"' ");
bvo.setCmaterialvid(materialoid); //物料
String batch = (String) LinkMidDataUtil.getHypubbo().findColValue("scm_batchcode", "pk_batchcode", " vbatchcode = '"+value2+"' and cmaterialoid = '"+materialoid+"' ");
bvo.setPk_batchcode(batch); //批次
if(value3 == null || value3.isEmpty()){
String nonhandnum = (String) LinkMidDataUtil.getHypubbo().findColValue("ic_onhandnum", "to_char(nonhandnum)", " pk_onhanddim = (select pk_onhanddim from ic_onhanddim where pk_batchcode = '"+batch+"' and cmaterialoid = '"+materialoid+"') ");
bvo.setNassistnum(nonhandnum == null ? UFDouble.ZERO_DBL : new UFDouble(nonhandnum)); //实收数量
bvo.setNnum(bvo.getNassistnum()); //实收主数量
bvo.setNshouldassistnum(bvo.getNassistnum()); //应收数量
bvo.setNshouldnum(bvo.getNassistnum()); //应收主数量
}else{
bvo.setNassistnum(value3 == null ? UFDouble.ZERO_DBL : new UFDouble(value3)); //实收数量
bvo.setNnum(bvo.getNassistnum()); //实收主数量
bvo.setNshouldassistnum(bvo.getNassistnum()); //应收数量
bvo.setNshouldnum(bvo.getNassistnum()); //应收主数量
}
bvoList.add(bvo);
}
aggvo.setChildrenVO(bvoList.toArray(new TransInBodyVO[bvoList.size()]));
this.getModel().directlyUpdate(aggvo);
之前项目上用到的,java读取excel数据,实体类换成你自己的 直接用