Excel出现#VALUE!举例解释 ols基本假设子解释一下,理论听不太懂!谢谢!

java(85)
目前处理Excel的开源javaAPI主要有两种,一是Jxl(Java Excel API),Jxl只支持Excel2003以下的版本。另外一种是Apache的Jakarta POI,相比于Jxl,POI对微软办公文档的支持更加强大,但是它使用复杂,上手慢。POI可支持更高的Excel版本2007。对Excel的读取,POI有两种模式,一是用户模式,这种方式同Jxl的使用很类似,使用简单,都是将文件一次性读到内存,文件小的时候,没有什么问题,当文件大的时候,就会出现OutOfMemory的内存溢出问题。第二种是事件驱动模式,拿Excel2007来说,其内容采用XML的格式来存储,所以处理excel就是解析XML,而目前使用事件驱动模式解析XML的API是SAX(Simple
API for XML),这种模型在读取XML文档时,并没有将整个文档读入内存,而是按顺序将整个文档解析完,在解析过程中,会主动产生事件交给程序中相应的处理函数来处理当前内容。因此这种方式对系统资源要求不高,可以处理海量数据。笔者曾经做过测试,这种方法处理一千万条,每条五列的数据花费大约11分钟。可见处理海量数据的文件事件驱动是一个很好的方式。而本文中用到的AbstractExcel2003Reader、AbstractExcel2007Reader对Excel的读取都是采用这种POI的事件驱动模式。至于Excel的写操作,对较高版本的Excel2007,POI提供了很好的支持,主要流程是第一步构建工作薄和电子表格对象,第二步在一个流中构建文本文件,第三步使用流中产生的数据替换模板中的电子表格。这种方式也可以处理海量数据文件。AbstractExcel2007Writer就是使用这种方式进行写操作。对于写入较低版本的Excel2003,POI使用了用户模式来处理,就是将整个文档加载进内存,如果数据量大的话就会出现内存溢出的问题,Excel2003Writer就是使用这种方式。据笔者的测试,如果数据量大于3万条,每条8列的话,就会报OutOfMemory的错误。Excel2003中每个电子表格的记录数必须在65536以下,否则就会发生异常。目前还没有好的解决方案,建议对于海量数据写入操作,尽量使用Excel2007。
&&&&&&public&&class&Excel2003Reader&implements&HSSFListener{&&&&&&private&int&minColumns&=&-1;&&&&&&private&POIFSFileSystem&&&&&&&private&int&lastRowN&&&&&&private&int&lastColumnN&&&&&&&&&&&&&&private&boolean&outputFormulaValues&=&true;&&&&&&&&&&&&&&private&SheetRecordCollectingListener&workbookBuildingL&&&&&&&&&&&&private&HSSFWorkbook&stubW&&&&&&&&&&&&&&private&SSTRecord&sstR&&&&&&private&FormatTrackingHSSFListener&formatL&&&&&&&&&&&&&&private&int&sheetIndex&=&-1;&&&&&&private&BoundSheetRecord[]&orderedBSRs;&&&&&&@SuppressWarnings(&unchecked&)&&&&&&private&ArrayList&boundSheetRecords&=&new&ArrayList();&&&&&&&&&&&&&&private&int&nextR&&&&&&private&int&nextC&&&&&&private&boolean&outputNextStringR&&&&&&&&&&&&private&int&curRow&=&0;&&&&&&&&&&&&private&List&String&&rowlist&=&new&ArrayList&String&();;&&&&&&@SuppressWarnings(&&unused&)&&&&&&private&String&sheetN&&&&&&&&&&&&private&IRowReader&rowR&&&&&&&&&&&&&&public&void&setRowReader(IRowReader&rowReader){&&&&&&&&&&this.rowReader&=&rowR&&&&&&}&&&&&&&&&&&&&&&&&&&&&public&void&process(String&fileName)&throws&IOException&{&&&&&&&&&&this.fs&=&new&POIFSFileSystem(new&FileInputStream(fileName));&&&&&&&&&&MissingRecordAwareHSSFListener&listener&=&new&MissingRecordAwareHSSFListener(&&&&&&&&&&&&&&&&&&this);&&&&&&&&&&formatListener&=&new&FormatTrackingHSSFListener(listener);&&&&&&&&&&HSSFEventFactory&factory&=&new&HSSFEventFactory();&&&&&&&&&&HSSFRequest&request&=&new&HSSFRequest();&&&&&&&&&&if&(outputFormulaValues)&{&&&&&&&&&&&&&&request.addListenerForAllRecords(formatListener);&&&&&&&&&&}&else&{&&&&&&&&&&&&&&workbookBuildingListener&=&new&SheetRecordCollectingListener(&&&&&&&&&&&&&&&&&&&&&&formatListener);&&&&&&&&&&&&&&request.addListenerForAllRecords(workbookBuildingListener);&&&&&&&&&&}&&&&&&&&&&factory.processWorkbookEvents(request,&fs);&&&&&&}&&&&&&&&&&&&&&&&&&&&@SuppressWarnings(&unchecked&)&&&&&&public&void&processRecord(Record&record)&{&&&&&&&&&&int&thisRow&=&-1;&&&&&&&&&&int&thisColumn&=&-1;&&&&&&&&&&String&thisStr&=&null;&&&&&&&&&&String&value&=&null;&&&&&&&&&&switch&(record.getSid())&{&&&&&&&&&&&&&&case&BoundSheetRecord.sid:&&&&&&&&&&&&&&&&&&boundSheetRecords.add(record);&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&case&BOFRecord.sid:&&&&&&&&&&&&&&&&&&BOFRecord&br&=&(BOFRecord)&&&&&&&&&&&&&&&&&&&if&(br.getType()&==&BOFRecord.TYPE_WORKSHEET)&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&if&(workbookBuildingListener&!=&null&&&&stubWorkbook&==&null)&{&&&&&&&&&&&&&&&&&&&&&&&&&&stubWorkbook&=&workbookBuildingListener&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&.getStubHSSFWorkbook();&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&sheetIndex++;&&&&&&&&&&&&&&&&&&&&&&if&(orderedBSRs&==&null)&{&&&&&&&&&&&&&&&&&&&&&&&&&&orderedBSRs&=&BoundSheetRecord&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&.orderByBofPosition(boundSheetRecords);&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&sheetName&=&orderedBSRs[sheetIndex].getSheetname();&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&&&&&&&case&SSTRecord.sid:&&&&&&&&&&&&&&&&&&sstRecord&=&(SSTRecord)&&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&&&&&&&case&BlankRecord.sid:&&&&&&&&&&&&&&&&&&BlankRecord&brec&=&(BlankRecord)&&&&&&&&&&&&&&&&&&&thisRow&=&brec.getRow();&&&&&&&&&&&&&&&&&&thisColumn&=&brec.getColumn();&&&&&&&&&&&&&&&&&&thisStr&=&&&;&&&&&&&&&&&&&&&&&&rowlist.add(thisColumn,&thisStr);&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&case&BoolErrRecord.sid:&&&&&&&&&&&&&&&&&&&BoolErrRecord&berec&=&(BoolErrRecord)&&&&&&&&&&&&&&&&&&&thisRow&=&berec.getRow();&&&&&&&&&&&&&&&&&&thisColumn&=&berec.getColumn();&&&&&&&&&&&&&&&&&&thisStr&=&berec.getBooleanValue()+&&;&&&&&&&&&&&&&&&&&&rowlist.add(thisColumn,&thisStr);&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&&&&&&&case&FormulaRecord.sid:&&&&&&&&&&&&&&&&&&&FormulaRecord&frec&=&(FormulaRecord)&&&&&&&&&&&&&&&&&&&thisRow&=&frec.getRow();&&&&&&&&&&&&&&&&&&thisColumn&=&frec.getColumn();&&&&&&&&&&&&&&&&&&if&(outputFormulaValues)&{&&&&&&&&&&&&&&&&&&&&&&if&(Double.isNaN(frec.getValue()))&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&outputNextStringRecord&=&true;&&&&&&&&&&&&&&&&&&&&&&&&&&nextRow&=&frec.getRow();&&&&&&&&&&&&&&&&&&&&&&&&&&nextColumn&=&frec.getColumn();&&&&&&&&&&&&&&&&&&&&&&}&else&{&&&&&&&&&&&&&&&&&&&&&&&&&&thisStr&=&formatListener.formatNumberDateCell(frec);&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&}&else&{&&&&&&&&&&&&&&&&&&&&&&thisStr&=&'&'&+&HSSFFormulaParser.toFormulaString(stubWorkbook,&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&frec.getParsedExpression())&+&'&';&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&rowlist.add(thisColumn,thisStr);&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&case&StringRecord.sid:&&&&&&&&&&&&&&&&&&if&(outputNextStringRecord)&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&StringRecord&srec&=&(StringRecord)&&&&&&&&&&&&&&&&&&&&&&&thisStr&=&srec.getString();&&&&&&&&&&&&&&&&&&&&&&thisRow&=&nextR&&&&&&&&&&&&&&&&&&&&&&thisColumn&=&nextC&&&&&&&&&&&&&&&&&&&&&&outputNextStringRecord&=&false;&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&case&LabelRecord.sid:&&&&&&&&&&&&&&&&&&LabelRecord&lrec&=&(LabelRecord)&&&&&&&&&&&&&&&&&&&curRow&=&thisRow&=&lrec.getRow();&&&&&&&&&&&&&&&&&&thisColumn&=&lrec.getColumn();&&&&&&&&&&&&&&&&&&value&=&lrec.getValue().trim();&&&&&&&&&&&&&&&&&&value&=&value.equals(&&)?&&&:&&&&&&&&&&&&&&&&&&this.rowlist.add(thisColumn,&value);&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&case&LabelSSTRecord.sid:&&&&&&&&&&&&&&&&&&&&LabelSSTRecord&lsrec&=&(LabelSSTRecord)&&&&&&&&&&&&&&&&&&&curRow&=&thisRow&=&lsrec.getRow();&&&&&&&&&&&&&&&&&&thisColumn&=&lsrec.getColumn();&&&&&&&&&&&&&&&&&&if&(sstRecord&==&null)&{&&&&&&&&&&&&&&&&&&&&&&rowlist.add(thisColumn,&&&&);&&&&&&&&&&&&&&&&&&}&else&{&&&&&&&&&&&&&&&&&&&&&&value&=&&sstRecord&&&&&&&&&&&&&&&&&&&&&&.getString(lsrec.getSSTIndex()).toString().trim();&&&&&&&&&&&&&&&&&&&&&&value&=&value.equals(&&)?&&&:&&&&&&&&&&&&&&&&&&&&&&rowlist.add(thisColumn,value);&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&case&NumberRecord.sid:&&&&&&&&&&&&&&&&&&&&NumberRecord&numrec&=&(NumberRecord)&&&&&&&&&&&&&&&&&&&curRow&=&thisRow&=&numrec.getRow();&&&&&&&&&&&&&&&&&&thisColumn&=&numrec.getColumn();&&&&&&&&&&&&&&&&&&value&=&formatListener.formatNumberDateCell(numrec).trim();&&&&&&&&&&&&&&&&&&value&=&value.equals(&&)?&&&:&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&rowlist.add(thisColumn,&value);&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&&&&&default:&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&if&(thisRow&!=&-1&&&&thisRow&!=&lastRowNumber)&{&&&&&&&&&&&&&&lastColumnNumber&=&-1;&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&if&(record&instanceof&MissingCellDummyRecord)&{&&&&&&&&&&&&&&MissingCellDummyRecord&mc&=&(MissingCellDummyRecord)&&&&&&&&&&&&&&&curRow&=&thisRow&=&mc.getRow();&&&&&&&&&&&&&&thisColumn&=&mc.getColumn();&&&&&&&&&&&&&&rowlist.add(thisColumn,&&&);&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&if&(thisRow&&&-1)&&&&&&&&&&&&&&lastRowNumber&=&thisR&&&&&&&&&&if&(thisColumn&&&-1)&&&&&&&&&&&&&&lastColumnNumber&=&thisC&&&&&&&&&&&&&&&&&&&&&&if&(record&instanceof&LastCellOfRowDummyRecord)&{&&&&&&&&&&&&&&if&(minColumns&&&0)&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&if&(lastColumnNumber&==&-1)&{&&&&&&&&&&&&&&&&&&&&&&lastColumnNumber&=&0;&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&}&&&&&&&&&&&&&&lastColumnNumber&=&-1;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&rowReader.getRows(sheetIndex,curRow,&rowlist);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&rowlist.clear();&&&&&&&&&&}&&&&&&}&&&&&&&&}&&
&&&&&&&public&class&Excel2007Reader&extends&DefaultHandler&{&&&&&&&&&&&&private&SharedStringsTable&&&&&&&&&&&&&private&String&lastC&&&&&&private&boolean&nextIsS&&&&&&&&private&int&sheetIndex&=&-1;&&&&&&private&List&String&&rowlist&=&new&ArrayList&String&();&&&&&&&&&&&&private&int&curRow&=&0;&&&&&&&&&&&&private&int&curCol&=&0;&&&&&&&&&&&&private&boolean&dateF&&&&&&&&&&&&private&boolean&numberF&&&&&&&&&&&&private&boolean&isTE&&&&&&&&&&&&private&IRowReader&rowR&&&&&&&&&&&&public&void&setRowReader(IRowReader&rowReader){&&&&&&&&&&this.rowReader&=&rowR&&&&&&}&&&&&&&&&&&&&&&&&&&&&&public&void&processOneSheet(String&filename,int&sheetId)&throws&Exception&{&&&&&&&&&&OPCPackage&pkg&=&OPCPackage.open(filename);&&&&&&&&&&XSSFReader&r&=&new&XSSFReader(pkg);&&&&&&&&&&SharedStringsTable&sst&=&r.getSharedStringsTable();&&&&&&&&&&XMLReader&parser&=&fetchSheetParser(sst);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&InputStream&sheet2&=&r.getSheet(&rId&+sheetId);&&&&&&&&&&sheetIndex++;&&&&&&&&&&InputSource&sheetSource&=&new&InputSource(sheet2);&&&&&&&&&&parser.parse(sheetSource);&&&&&&&&&&sheet2.close();&&&&&&}&&&&&&&&&&&&&&&&&&public&void&process(String&filename)&throws&Exception&{&&&&&&&&&&OPCPackage&pkg&=&OPCPackage.open(filename);&&&&&&&&&&XSSFReader&r&=&new&XSSFReader(pkg);&&&&&&&&&&SharedStringsTable&sst&=&r.getSharedStringsTable();&&&&&&&&&&XMLReader&parser&=&fetchSheetParser(sst);&&&&&&&&&&Iterator&InputStream&&sheets&=&r.getSheetsData();&&&&&&&&&&while&(sheets.hasNext())&{&&&&&&&&&&&&&&curRow&=&0;&&&&&&&&&&&&&&sheetIndex++;&&&&&&&&&&&&&&InputStream&sheet&=&sheets.next();&&&&&&&&&&&&&&InputSource&sheetSource&=&new&InputSource(sheet);&&&&&&&&&&&&&&parser.parse(sheetSource);&&&&&&&&&&&&&&sheet.close();&&&&&&&&&&}&&&&&&}&&&&&&&&public&XMLReader&fetchSheetParser(SharedStringsTable&sst)&&&&&&&&&&&&&&throws&SAXException&{&&&&&&&&&&XMLReader&parser&=&XMLReaderFactory&&&&&&&&&&&&&&&&&&.createXMLReader(&org.apache.xerces.parsers.SAXParser&);&&&&&&&&&&this.sst&=&&&&&&&&&&&parser.setContentHandler(this);&&&&&&&&&&return&&&&&&&}&&&&&&&&public&void&startElement(String&uri,&String&localName,&String&name,&&&&&&&&&&&&&&Attributes&attributes)&throws&SAXException&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&if&(&c&.equals(name))&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&String&cellType&=&attributes.getValue(&t&);&&&&&&&&&&&&&&if&(&s&.equals(cellType))&{&&&&&&&&&&&&&&&&&&nextIsString&=&true;&&&&&&&&&&&&&&}&else&{&&&&&&&&&&&&&&&&&&nextIsString&=&false;&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&String&cellDateType&=&attributes.getValue(&s&);&&&&&&&&&&&&&&if&(&1&.equals(cellDateType)){&&&&&&&&&&&&&&&&&&dateFlag&=&true;&&&&&&&&&&&&&&}&else&{&&&&&&&&&&&&&&&&&&dateFlag&=&false;&&&&&&&&&&&&&&}&&&&&&&&&&&&&&String&cellNumberType&=&attributes.getValue(&s&);&&&&&&&&&&&&&&if(&2&.equals(cellNumberType)){&&&&&&&&&&&&&&&&&&numberFlag&=&true;&&&&&&&&&&&&&&}&else&{&&&&&&&&&&&&&&&&&&numberFlag&=&false;&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&if(&t&.equals(name)){&&&&&&&&&&&&&&isTElement&=&true;&&&&&&&&&&}&else&{&&&&&&&&&&&&&&isTElement&=&false;&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&lastContents&=&&&;&&&&&&}&&&&&&&&public&void&endElement(String&uri,&String&localName,&String&name)&&&&&&&&&&&&&&throws&SAXException&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&if&(nextIsString)&{&&&&&&&&&&&&&&try&{&&&&&&&&&&&&&&&&&&int&idx&=&Integer.parseInt(lastContents);&&&&&&&&&&&&&&&&&&lastContents&=&new&XSSFRichTextString(sst.getEntryAt(idx))&&&&&&&&&&&&&&&&&&&&&&&&&&.toString();&&&&&&&&&&&&&&}&catch&(Exception&e)&{&&&&&&&&&&&&&&&&}&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&if(isTElement){&&&&&&&&&&&&&&String&value&=&lastContents.trim();&&&&&&&&&&&&&&rowlist.add(curCol,&value);&&&&&&&&&&&&&&curCol++;&&&&&&&&&&&&&&isTElement&=&false;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&else&if&(&v&.equals(name))&{&&&&&&&&&&&&&&String&value&=&lastContents.trim();&&&&&&&&&&&&&&value&=&value.equals(&&)?&&&:&&&&&&&&&&&&&&&&&&&&&&&&&&&&if(dateFlag){&&&&&&&&&&&&&&&&&&&Date&date&=&HSSFDateUtil.getJavaDate(Double.valueOf(value));&&&&&&&&&&&&&&&&&&&SimpleDateFormat&dateFormat&=&new&SimpleDateFormat(&&&&&&&&&&&&&&&&&&&&dd/MM/yyyy&);&&&&&&&&&&&&&&&&&&&value&=&dateFormat.format(date);&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&if(numberFlag){&&&&&&&&&&&&&&&&&&BigDecimal&bd&=&new&BigDecimal(value);&&&&&&&&&&&&&&&&&&value&=&bd.setScale(3,BigDecimal.ROUND_UP).toString();&&&&&&&&&&&&&&}&&&&&&&&&&&&&&rowlist.add(curCol,&value);&&&&&&&&&&&&&&curCol++;&&&&&&&&&&}else&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&if&(name.equals(&row&))&{&&&&&&&&&&&&&&&&&&rowReader.getRows(sheetIndex,curRow,rowlist);&&&&&&&&&&&&&&&&&&rowlist.clear();&&&&&&&&&&&&&&&&&&curRow++;&&&&&&&&&&&&&&&&&&curCol&=&0;&&&&&&&&&&&&&&}&&&&&&&&&&}&&&&&&&&&&&&&&&&}&&&&&&&&public&void&characters(char[]&ch,&int&start,&int&length)&&&&&&&&&&&&&&throws&SAXException&{&&&&&&&&&&&&&&&&&&&&lastContents&+=&new&String(ch,&start,&length);&&&&&&}&&}&&
public&class&ExcelReaderUtil&{&&&&&&&&&&&&&&&&&&public&static&final&String&EXCEL03_EXTENSION&=&&.xls&;&&&&&&&&&&&&public&static&final&String&EXCEL07_EXTENSION&=&&.xlsx&;&&&&&&&&&&&&&&&&&&&&&&&&public&static&void&readExcel(IRowReader&reader,String&fileName)&throws&Exception{&&&&&&&&&&&&&&&&&&&&if&(fileName.endsWith(EXCEL03_EXTENSION)){&&&&&&&&&&&&&&Excel2003Reader&excel03&=&new&Excel2003Reader();&&&&&&&&&&&&&&excel03.setRowReader(reader);&&&&&&&&&&&&&&excel03.process(fileName);&&&&&&&&&&&&&&&&&&&&}&else&if&(fileName.endsWith(EXCEL07_EXTENSION)){&&&&&&&&&&&&&&Excel2007Reader&excel07&=&new&Excel2007Reader();&&&&&&&&&&&&&&excel07.setRowReader(reader);&&&&&&&&&&&&&&excel07.process(fileName);&&&&&&&&&&}&else&{&&&&&&&&&&&&&&throw&new&&Exception(&文件格式错误,fileName的扩展名只能是xls或xlsx。&);&&&&&&&&&&}&&&&&&}&&}&&
public&interface&IRowReader&{&&&&&&&&&&&&&&&&&&&&&&public&&void&getRows(int&sheetIndex,int&curRow,&List&String&&rowlist);&&}&&
public&class&RowReader&implements&IRowReader{&&&&&&&&&&&&&&&&&&public&void&getRows(int&sheetIndex,&int&curRow,&List&String&&rowlist)&{&&&&&&&&&&&&&&&&&&&&System.out.print(curRow+&&&);&&&&&&&&&&for&(int&i&=&0;&i&&&rowlist.size();&i++)&{&&&&&&&&&&&&&&System.out.print(rowlist.get(i)&+&&&&);&&&&&&&&&&}&&&&&&&&&&System.out.println();&&&&&&}&&&&}&&
public&class&Main&{&&&&&&&&&&&&public&static&void&main(String[]&args)&throws&Exception&{&&&&&&&&&&IRowReader&reader&=&new&RowReader();&&&&&&&&&&&&&&&&&&&&ExcelReaderUtil.readExcel(reader,&&F://test07.xlsx&);&&&&&&}&&}&&
public&class&Excel2003Writer&{&&&&&&&&&&&&&&&&public&static&void&main(String[]&args)&{&&&&&&&&&&try{&&&&&&&&&&&&&&&&&&System.out.println(&开始写入excel2003....&);&&&&&&&&&&&&&&writeExcel(&tes2003.xls&);&&&&&&&&&&&&&&System.out.println(&写完xcel2003&);&&&&&&&&&&}&catch&(IOException&e)&{&&&&&&&&&&&&&&&&&&&&}&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&public&static&void&writeExcel(String&fileName)&throws&IOException{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Workbook&wb&=&new&HSSFWorkbook();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&FileOutputStream&fileOut&=&new&FileOutputStream(fileName);&&&&&&&&&&&&&&&&&&&&&&&&&&&&Sheet&sheet&=&wb.createSheet(&newsheet&);&&&&&&&&&&&&&&&&&&&&&&&&&&&&for(int&i=0;i&20000;i++){&&&&&&&&&&&&&&&&&&Row&row&=&sheet.createRow(i);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Cell&cell&=&row.createCell(0);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&cell.setCellValue(1);&&&&&&&&&&&&&&&&&&row.createCell(1).setCellValue(1+i);&&&&&&&&&&&&&&&&&&row.createCell(2).setCellValue(true);&&&&&&&&&&&&&&&&&&row.createCell(3).setCellValue(0.43d);&&&&&&&&&&&&&&&&&&row.createCell(4).setCellValue('d');&&&&&&&&&&&&&&&&&&row.createCell(5).setCellValue(&&);&&&&&&&&&&&&&&&&&&row.createCell(6).setCellValue(&第七列&+i);&&&&&&&&&&&&&&&&&&row.createCell(7).setCellValue(&第八列&+i);&&&&&&&&&&&&&&}&&&&&&&&&&&&&&wb.write(fileOut);&&&&&&&&&&&&&&fileOut.close();&&&&&&}&&&&&&}&&
&&&&&&public&abstract&class&AbstractExcel2007Writer&{&&&&&&&&&&&&private&SpreadsheetWriter&&&&&&&&&&&&&&&&&&&public&void&process(String&fileName)&throws&Exception{&&&&&&&&&&&&&&&&&&&&XSSFWorkbook&wb&=&new&XSSFWorkbook();&&&&&&&&&&XSSFSheet&sheet&=&wb.createSheet(&sheet1&);&&&&&&&&&&&&&&&&&&&&String&sheetRef&=&sheet.getPackagePart().getPartName().getName();&&&&&&&&&&&&&&&&&&&&&&FileOutputStream&os&=&new&FileOutputStream(&template.xlsx&);&&&&&&&&&&wb.write(os);&&&&&&&&&&os.close();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&File&tmp&=&File.createTempFile(&sheet&,&&.xml&);&&&&&&&&&&Writer&fw&=&new&FileWriter(tmp);&&&&&&&&&&sw&=&new&SpreadsheetWriter(fw);&&&&&&&&&&generate();&&&&&&&&&&fw.close();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&File&templateFile&=&new&File(&template.xlsx&);&&&&&&&&&&FileOutputStream&out&=&new&FileOutputStream(fileName);&&&&&&&&&&substitute(templateFile,&tmp,&sheetRef.substring(1),&out);&&&&&&&&&&out.close();&&&&&&&&&&&&&&&&&&&&System.gc();&&&&&&&&&&&&&&&&&&&&if&(templateFile.isFile()&&templateFile.exists()){&&&&&&&&&&&&&&templateFile.delete();&&&&&&&&&&}&&&&&&}&&&&&&&&&&&&&&&&&public&abstract&void&generate()&throws&E&&&&&&&&public&void&beginSheet()&throws&IOException&{&&&&&&&&&&sw.beginSheet();&&&&&&}&&&&&&&&public&void&insertRow(int&rowNum)&throws&IOException&{&&&&&&&&&&sw.insertRow(rowNum);&&&&&&}&&&&&&&&public&void&createCell(int&columnIndex,&String&value)&throws&IOException&{&&&&&&&&&&sw.createCell(columnIndex,&value,&-1);&&&&&&}&&&&&&&&public&void&createCell(int&columnIndex,&double&value)&throws&IOException&{&&&&&&&&&&sw.createCell(columnIndex,&value,&-1);&&&&&&}&&&&&&&&public&void&endRow()&throws&IOException&{&&&&&&&&&&sw.endRow();&&&&&&}&&&&&&&&public&void&endSheet()&throws&IOException&{&&&&&&&&&&sw.endSheet();&&&&&&}&&&&&&&&&&&&&&&&&&&&private&static&void&substitute(File&zipfile,&File&tmpfile,&String&entry,&&&&&&&&&&&&&&OutputStream&out)&throws&IOException&{&&&&&&&&&&ZipFile&zip&=&new&ZipFile(zipfile);&&&&&&&&&&ZipOutputStream&zos&=&new&ZipOutputStream(out);&&&&&&&&&&&&@SuppressWarnings(&unchecked&)&&&&&&&&&&Enumeration&ZipEntry&&en&=&(Enumeration&ZipEntry&)&zip.entries();&&&&&&&&&&while&(en.hasMoreElements())&{&&&&&&&&&&&&&&ZipEntry&ze&=&en.nextElement();&&&&&&&&&&&&&&if&(!ze.getName().equals(entry))&{&&&&&&&&&&&&&&&&&&zos.putNextEntry(new&ZipEntry(ze.getName()));&&&&&&&&&&&&&&&&&&InputStream&is&=&zip.getInputStream(ze);&&&&&&&&&&&&&&&&&&copyStream(is,&zos);&&&&&&&&&&&&&&&&&&is.close();&&&&&&&&&&&&&&}&&&&&&&&&&}&&&&&&&&&&zos.putNextEntry(new&ZipEntry(entry));&&&&&&&&&&InputStream&is&=&new&FileInputStream(tmpfile);&&&&&&&&&&copyStream(is,&zos);&&&&&&&&&&is.close();&&&&&&&&&&zos.close();&&&&&&}&&&&&&&&private&static&void&copyStream(InputStream&in,&OutputStream&out)&&&&&&&&&&&&&&throws&IOException&{&&&&&&&&&&byte[]&chunk&=&new&byte[1024];&&&&&&&&&&int&&&&&&&&&&&while&((count&=&in.read(chunk))&&=&0)&{&&&&&&&&&&&&&&out.write(chunk,&0,&count);&&&&&&&&&&}&&&&&&}&&&&&&&&&&&&&&&&&public&static&class&SpreadsheetWriter&{&&&&&&&&&&private&final&Writer&_&&&&&&&&&&private&int&_&&&&&&&&&&private&static&String&LINE_SEPARATOR&=&System.getProperty(&line.separator&);&&&&&&&&&&&&public&SpreadsheetWriter(Writer&out)&{&&&&&&&&&&&&&&_out&=&&&&&&&&&&&}&&&&&&&&&&&&public&void&beginSheet()&throws&IOException&{&&&&&&&&&&&&&&_out.write(&&?xml&version=\&1.0\&&encoding=\&GB2312\&?&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&+&&&worksheet&xmlns=\&http://schemas.openxmlformats.org/spreadsheetml/2006/main\&&&);&&&&&&&&&&&&&&_out.write(&&sheetData&&+LINE_SEPARATOR);&&&&&&&&&&}&&&&&&&&&&&&public&void&endSheet()&throws&IOException&{&&&&&&&&&&&&&&_out.write(&&/sheetData&&);&&&&&&&&&&&&&&_out.write(&&/worksheet&&);&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&public&void&insertRow(int&rownum)&throws&IOException&{&&&&&&&&&&&&&&_out.write(&&row&r=\&&&+&(rownum&+&1)&+&&\&&&+LINE_SEPARATOR);&&&&&&&&&&&&&&this._rownum&=&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&public&void&endRow()&throws&IOException&{&&&&&&&&&&&&&&_out.write(&&/row&&+LINE_SEPARATOR);&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&public&void&createCell(int&columnIndex,&String&value,&int&styleIndex)&&&&&&&&&&&&&&&&&&throws&IOException&{&&&&&&&&&&&&&&String&ref&=&new&CellReference(_rownum,&columnIndex)&&&&&&&&&&&&&&&&&&&&&&.formatAsString();&&&&&&&&&&&&&&_out.write(&&c&r=\&&&+&ref&+&&\&&t=\&inlineStr\&&);&&&&&&&&&&&&&&if&(styleIndex&!=&-1)&&&&&&&&&&&&&&&&&&_out.write(&&s=\&&&+&styleIndex&+&&\&&);&&&&&&&&&&&&&&_out.write(&&&);&&&&&&&&&&&&&&_out.write(&&is&&t&&+XMLEncoder.encode(value)+&&/t&&/is&&);&&&&&&&&&&&&&&_out.write(&&/c&&);&&&&&&&&&&}&&&&&&&&&&&&public&void&createCell(int&columnIndex,&String&value)&&&&&&&&&&&&&&&&&&throws&IOException&{&&&&&&&&&&&&&&createCell(columnIndex,&value,&-1);&&&&&&&&&&}&&&&&&&&&&&&public&void&createCell(int&columnIndex,&double&value,&int&styleIndex)&&&&&&&&&&&&&&&&&&throws&IOException&{&&&&&&&&&&&&&&String&ref&=&new&CellReference(_rownum,&columnIndex)&&&&&&&&&&&&&&&&&&&&&&.formatAsString();&&&&&&&&&&&&&&_out.write(&&c&r=\&&&+&ref&+&&\&&t=\&n\&&);&&&&&&&&&&&&&&if&(styleIndex&!=&-1)&&&&&&&&&&&&&&&&&&_out.write(&&s=\&&&+&styleIndex&+&&\&&);&&&&&&&&&&&&&&_out.write(&&&);&&&&&&&&&&&&&&_out.write(&&v&&&+&value&+&&&/v&&);&&&&&&&&&&&&&&_out.write(&&/c&&);&&&&&&&&&&}&&&&&&&&&&&&public&void&createCell(int&columnIndex,&double&value)&&&&&&&&&&&&&&&&&&throws&IOException&{&&&&&&&&&&&&&&createCell(columnIndex,&value,&-1);&&&&&&&&&&}&&&&&&&&&&&&public&void&createCell(int&columnIndex,&Calendar&value,&int&styleIndex)&&&&&&&&&&&&&&&&&&throws&IOException&{&&&&&&&&&&&&&&createCell(columnIndex,&DateUtil.getExcelDate(value,&false),&&&&&&&&&&&&&&&&&&&&&&styleIndex);&&&&&&&&&&}&&&&&&}&&}&&
public&class&Excel2007WriterImpl&extends&AbstractExcel2007Writer{&&&&&&&&&&&&&&&&&&&&&&&public&static&void&main(String[]&args)&throws&Exception&{&&&&&&&&&&&&&&&&&&&&System.out.println(&............................&);&&&&&&&&&&long&start&=&System.currentTimeMillis();&&&&&&&&&&&&&&&&&&&&AbstractExcel2007Writer&excel07Writer&=&new&Excel2007WriterImpl();&&&&&&&&&&&&&&&&&&&&excel07Writer.process(&F://test07.xlsx&);&&&&&&&&&&long&end&=&System.currentTimeMillis();&&&&&&&&&&System.out.println(&.....................&+(end-start)/1000);&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&@Override&&&&&&public&void&generate()throws&Exception&{&&&&&&&&&&&&&&&&&&&&beginSheet();&&&&&&&&&&for&(int&rownum&=&0;&rownum&&&100;&rownum++)&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&insertRow(rownum);&&&&&&&&&&&&&&&&&&&&&&&&&&&&createCell(0,&&中国&&&+&rownum&+&&!&);&&&&&&&&&&&&&&createCell(1,&789);&&&&&&&&&&&&&&createCell(2,&&23.67%&);&&&&&&&&&&&&&&createCell(3,&&12:12:23&);&&&&&&&&&&&&&&createCell(4,&&&12:12:23&);&&&&&&&&&&&&&&createCell(5,&&true&);&&&&&&&&&&&&&&createCell(6,&&false&);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&endRow();&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&endSheet();&&&&&&}&&&&}&&
public&class&XMLEncoder&{&&&&&&&&private&static&final&String[]&xmlCode&=&new&String[256];&&&&&&&&static&{&&&&&&&&&&&&&&&&&&&&xmlCode['\'']&=&&'&;&&&&&&&&&&xmlCode['\&']&=&&&&;&&&&&&&&&&&xmlCode['&']&=&&&&;&&&&&&&&&&&xmlCode['&']&=&&&&;&&&&&&&&&&&xmlCode['&']&=&&&&;&&&&&&&}&&&&&&&&&&&&&&&&&&&&&public&static&String&encode(String&string)&{&&&&&&&&&&if&(string&==&null)&return&&&;&&&&&&&&&&int&n&=&string.length();&&&&&&&&&&char&&&&&&&&&&&String&&&&&&&&&&&StringBuffer&buffer&=&new&StringBuffer();&&&&&&&&&&&&&&&&&&&&for&(int&i&=&0;&i&&&n;&i++)&{&&&&&&&&&&&&&&character&=&string.charAt(i);&&&&&&&&&&&&&&&&&&&&&&&&&&&&try&{&&&&&&&&&&&&&&&&&&xmlchar&=&xmlCode[character];&&&&&&&&&&&&&&&&&&if&(xmlchar&==&null)&{&&&&&&&&&&&&&&&&&&&&&&buffer.append(character);&&&&&&&&&&&&&&&&&&}&else&{&&&&&&&&&&&&&&&&&&&&&&buffer.append(xmlCode[character]);&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&}&catch&(ArrayIndexOutOfBoundsException&aioobe)&{&&&&&&&&&&&&&&&&&&buffer.append(character);&&&&&&&&&&&&&&}&&&&&&&&&&}&&&&&&&&&&return&buffer.toString();&&&&&&}&&&&}&&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:129050次
积分:2182
积分:2182
排名:第12547名
原创:29篇
转载:389篇
(1)(1)(1)(2)(12)(14)(9)(1)(1)(1)(2)(4)(6)(4)(8)(12)(8)(3)(5)(12)(18)(30)(15)(23)(30)(14)(17)(13)(56)(43)(18)(10)(16)(10)}

我要回帖

更多关于 举例解释最大实体要求 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信