java格式转化异常,java.text.ParseException:无法解析的日期:“ 42321”(这是关于将日期从excel格式转换为java格式,而不是有关DateFormat.parse。...

processOneSheet("C:/Users/Penchalaiah/Desktop/New folder/"+hs.getAttribute("filename1"));

System.out.println(hs.getAttribute("filename1"));

System.out.println("clossing the connnection");

ps.close();

con1.close();

System.out.println("execution completed");

//request.setAttribute("message","THE XLSX DATA TRANSFERRED SUCCEFULLY");

// request.getRequestDispatcher("/HomePage.jsp").forward(request, response);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

/*try {

ps.close();

con1.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}*/

}

public void processOneSheet(String filename) throws Exception {

System.out.println("executing Process Method");

OPCPackage pkg = OPCPackage.open(filename);

XSSFReader r = new XSSFReader( pkg );

SharedStringsTable sst = r.getSharedStringsTable();

XMLReader parser = fetchSheetParser(sst);

// To look up the Sheet Name / Sheet Order / rID,

//  you need to process the core Workbook stream.

// Normally it's of the form rId# or rSheet#

InputStream sheet2 = r.getSheet("rId2");

System.out.println("Sheet2");

InputSource sheetSource = new InputSource(sheet2);

parser.parse(sheetSource);

sheet2.close();

}

public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException {

XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");

ContentHandler handler = new SheetHandler(sst);

parser.setContentHandler(handler);

return parser;

}

/**

* See org.xml.sax.helpers.DefaultHandler javadocs

*/

private  class SheetHandler extends DefaultHandler {

private SharedStringsTable sst;

private String lastContents;

private boolean nextIsString;

String TxnNo;

String SurveyId;

Date date;

String Time;

String Lane;

String Avspeed;

String Direction;

String VehicleCategory;

String Axle_count;

String Vehicle;

int i = 1;

private SheetHandler(SharedStringsTable sst) {

this.sst = sst;

}

public void startElement(String uri, String localName, String name,

Attributes attributes) throws SAXException {

// c => cell

if(name.equals("c")) {

// Print the cell reference

//System.out.print(attributes.getValue("r") +" -");

// Figure out if the value is an index in the SST

String cellType = attributes.getValue("t");

if(cellType != null && cellType.equals("s")) {

nextIsString = true;

} else {

nextIsString = false;

}

}

// Clear contents cache

lastContents ="";

}

public void endElement(String uri, String localName, String name)

throws SAXException {

// Process the last contents as required.

// Do now, as characters() may be called more than once

if(nextIsString) {

int idx = Integer.parseInt(lastContents);

lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();

nextIsString = false;

}

// v => contents of a cell

// Output after we've seen the string contents

if(name.equals("v")) {

if(i == 1){

TxnNo = lastContents;

}

if(i == 2){

SurveyId = lastContents;

}

if(i == 3){

try {

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

date = new SimpleDateFormat("dd/MM/yyyy",Locale.ENGLISH).parse(lastContents);

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

System.out.println("The Date is:"+date);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//System.out.print(lastContents+"("+i+")");

}

if(i == 4){

Time = lastContents;

//System.out.print(lastContents+"("+i+")");

}

if(i == 5){

Lane = lastContents;

//System.out.print(lastContents+"("+i+")");

}

if(i == 6){

Avspeed = lastContents;

//System.out.print(lastContents+"("+i+")");

}

if(i == 7){

Direction = lastContents;

//System.out.print(lastContents+"("+i+")");

}

if(i == 8){

VehicleCategory = lastContents;

//System.out.print(lastContents+"("+i+")");

}

if(i == 9){

Axle_count = lastContents;

//System.out.print(lastContents+"("+i+")");

}

if(i == 10){

Vehicle = lastContents;

//System.out.print(lastContents+"("+i+")");

//System.out.print(lastContents+"("+i+")");

insertInToDb(TxnNo, SurveyId, date, Time, Lane, Avspeed,Direction, VehicleCategory, Axle_count, Vehicle);

i = 0;

}

i++;

}

}

public void characters(char[] ch, int start, int length)

throws SAXException {

lastContents += new String(ch, start, length);

}

}

int gcc = 0;

public void insertInToDb(String TxnNo,String SurveyId,Date date, String Time,String Lane,String Avspeed,String Direction, String VehicleCategory, String Axle_count, String Vehicle){

try {

ps.setString(1, TxnNo);

ps.setString(2, SurveyId);

ps.setDate(3, (java.sql.Date) date);

ps.setString(4, Time);

ps.setString(5, Lane);

ps.setString(6, Avspeed);

ps.setString(7, Direction);

ps.setString(8, VehicleCategory);

ps.setString(9, Axle_count);

ps.setString(10, Vehicle);

ps.setString(11, (String)hs.getAttribute("zoneId1"));

ps.setString(12, (String)hs.getAttribute("location1"));

ps.executeUpdate();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值