大数据量的excel文件读取——2003及之前版本(含代码及示例)

现在的任务就是把客户数据迁移到数据库中,由于客户提供的数据都存储在excel中,有些文件数据量还很大,在 usermodel模式下经常内存溢出,于是只能采用直接通过pl/sql往数据库复制或是用eventusermodel模式读取。直接复制倒是简单,但是速度太慢,一次复制的太多也会内存溢出。 

     usermodel模式对excel操作前需要将文件全部转入内存,对较大文件来说内存开销很大。但是其使用简单。

     eventusermodel模式采用事件模型,对文件边读取边处理,内存消耗较低,效率高,因为不用等待文件全部装入内存。但使用较复杂。

 

     excel读取采用的API为POI3.6,使用前先下载此包,若运行中出现其他依赖包不存在,请下载相应依赖包。

 

    下面展示的是excel2003及其之前版本的大文件读取方法。

 

抽象类 HxlsAbstract:

Java代码 复制代码
  1. package com.gaosheng.util.xls;   
  2. import java.io.FileInputStream;   
  3. import java.io.FileNotFoundException;   
  4. import java.io.IOException;   
  5. import java.io.PrintStream;   
  6. import java.sql.SQLException;   
  7. import java.util.ArrayList;   
  8. import java.util.List;   
  9.   
  10. import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;   
  11. import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;   
  12. import org.apache.poi.hssf.eventusermodel.HSSFListener;   
  13. import org.apache.poi.hssf.eventusermodel.HSSFRequest;   
  14. import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener;   
  15. import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder.SheetRecordCollectingListener;   
  16. import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;   
  17. import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord;   
  18. import org.apache.poi.hssf.model.HSSFFormulaParser;   
  19. import org.apache.poi.hssf.record.BOFRecord;   
  20. import org.apache.poi.hssf.record.BlankRecord;   
  21. import org.apache.poi.hssf.record.BoolErrRecord;   
  22. import org.apache.poi.hssf.record.BoundSheetRecord;   
  23. import org.apache.poi.hssf.record.FormulaRecord;   
  24. import org.apache.poi.hssf.record.LabelRecord;   
  25. import org.apache.poi.hssf.record.LabelSSTRecord;   
  26. import org.apache.poi.hssf.record.NoteRecord;   
  27. import org.apache.poi.hssf.record.NumberRecord;   
  28. import org.apache.poi.hssf.record.RKRecord;   
  29. import org.apache.poi.hssf.record.Record;   
  30. import org.apache.poi.hssf.record.SSTRecord;   
  31. import org.apache.poi.hssf.record.StringRecord;   
  32. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  33. import org.apache.poi.poifs.filesystem.POIFSFileSystem;   
  34.   
  35. public abstract class HxlsAbstract implements HSSFListener {   
  36.     private int minColumns;   
  37.     private POIFSFileSystem fs;   
  38.     private PrintStream output;   
  39.   
  40.     private int lastRowNumber;   
  41.     private int lastColumnNumber;   
  42.   
  43.     /** Should we output the formula, or the value it has? */  
  44.     private boolean outputFormulaValues = true;   
  45.   
  46.     /** For parsing Formulas */  
  47.     private SheetRecordCollectingListener workbookBuildingListener;   
  48.     private HSSFWorkbook stubWorkbook;   
  49.   
  50.     // Records we pick up as we process   
  51.     private SSTRecord sstRecord;   
  52.     private FormatTrackingHSSFListener formatListener;   
  53.   
  54.     /** So we known which sheet we're on */  
  55.     private int sheetIndex = -1;   
  56.     private BoundSheetRecord[] orderedBSRs;   
  57.     @SuppressWarnings("unchecked")   
  58.     private ArrayList boundSheetRecords = new ArrayList();   
  59.   
  60.     // For handling formulas with string results   
  61.     private int nextRow;   
  62.     private int nextColumn;   
  63.     private boolean outputNextStringRecord;   
  64.   
  65.     private int curRow;   
  66.     private List<String> rowlist;   
  67.     @SuppressWarnings"unused")   
  68.     private String sheetName;   
  69.   
  70.     public HxlsAbstract(POIFSFileSystem fs)   
  71.             throws SQLException {   
  72.         this.fs = fs;   
  73.         this.output = System.out;   
  74.         this.minColumns = -1;   
  75.         this.curRow = 0;   
  76.         this.rowlist = new ArrayList<String>();   
  77.     }   
  78.   
  79.     public HxlsAbstract(String filename) throws IOException,   
  80.             FileNotFoundException, SQLException {   
  81.         this(new POIFSFileSystem(new FileInputStream(filename)));   
  82.     }   
  83.        
  84.     //excel记录行操作方法,以行索引和行元素列表为参数,对一行元素进行操作,元素为String类型   
  85. //  public abstract void optRows(int curRow, List<String> rowlist) throws SQLException ;   
  86.        
  87.     //excel记录行操作方法,以sheet索引,行索引和行元素列表为参数,对sheet的一行元素进行操作,元素为String类型   
  88.     public abstract void optRows(int sheetIndex,int curRow, List<String> rowlist) throws SQLException;   
  89.        
  90.     /**  
  91.      * 遍历 excel 文件  
  92.      */  
  93.     public void process() throws IOException {   
  94.         MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(   
  95.                 this);   
  96.         formatListener = new FormatTrackingHSSFListener(listener);   
  97.   
  98.         HSSFEventFactory factory = new HSSFEventFactory();   
  99.         HSSFRequest request = new HSSFRequest();   
  100.   
  101.         if (outputFormulaValues) {   
  102.             request.addListenerForAllRecords(formatListener);   
  103.         } else {   
  104.             workbookBuildingListener = new SheetRecordCollectingListener(   
  105.                     formatListener);   
  106.             request.addListenerForAllRecords(workbookBuildingListener);   
  107.         }   
  108.   
  109.         factory.processWorkbookEvents(request, fs);   
  110.     }   
  111.        
  112.     /**  
  113.      * HSSFListener 监听方法,处理 Record  
  114.      */  
  115.     @SuppressWarnings("unchecked")   
  116.     public void processRecord(Record record) {   
  117.         int thisRow = -1;   
  118.         int thisColumn = -1;   
  119.         String thisStr = null;   
  120.         String value = null;   
  121.            
  122.         switch (record.getSid()) {   
  123.         case BoundSheetRecord.sid:   
  124.             boundSheetRecords.add(record);   
  125.             break;   
  126.         case BOFRecord.sid:   
  127.             BOFRecord br = (BOFRecord) record;   
  128.             if (br.getType() == BOFRecord.TYPE_WORKSHEET) {   
  129.                 // Create sub workbook if required   
  130.                 if (workbookBuildingListener != null && stubWorkbook == null) {   
  131.                     stubWorkbook = workbookBuildingListener   
  132.                             .getStubHSSFWorkbook();   
  133.                 }   
  134.   
  135.                 // Works by ordering the BSRs by the location of   
  136.                 // their BOFRecords, and then knowing that we   
  137.                 // process BOFRecords in byte offset order   
  138.                 sheetIndex++;   
  139.                 if (orderedBSRs == null) {   
  140.                     orderedBSRs = BoundSheetRecord   
  141.                             .orderByBofPosition(boundSheetRecords);   
  142.                 }   
  143.                 sheetName = orderedBSRs[sheetIndex].getSheetname();   
  144.             }   
  145.             break;   
  146.   
  147.         case SSTRecord.sid:   
  148.             sstRecord = (SSTRecord) record;   
  149.             break;   
  150.   
  151.         case BlankRecord.sid:   
  152.             BlankRecord brec = (BlankRecord) record;   
  153.   
  154.             thisRow = brec.getRow();   
  155.             thisColumn = brec.getColumn();   
  156.             thisStr = "";   
  157.             break;   
  158.         case BoolErrRecord.sid:   
  159.             BoolErrRecord berec = (BoolErrRecord) record;   
  160.   
  161.             thisRow = berec.getRow();   
  162.             thisColumn = berec.getColumn();   
  163.             thisStr = "";   
  164.             break;   
  165.   
  166.         case FormulaRecord.sid:   
  167.             FormulaRecord frec = (FormulaRecord) record;   
  168.   
  169.             thisRow = frec.getRow();   
  170.             thisColumn = frec.getColumn();   
  171.   
  172.             if (outputFormulaValues) {   
  173.                 if (Double.isNaN(frec.getValue())) {   
  174.                     // Formula result is a string   
  175.                     // This is stored in the next record   
  176.                     outputNextStringRecord = true;   
  177.                     nextRow = frec.getRow();   
  178.                     nextColumn = frec.getColumn();   
  179.                 } else {   
  180.                     thisStr = formatListener.formatNumberDateCell(frec);   
  181.                 }   
  182.             } else {   
  183.                 thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook,   
  184.                         frec.getParsedExpression()) + '"';   
  185.             }   
  186.             break;   
  187.         case StringRecord.sid:   
  188.             if (outputNextStringRecord) {   
  189.                 // String for formula   
  190.                 StringRecord srec = (StringRecord) record;   
  191.                 thisStr = srec.getString();   
  192.                 thisRow = nextRow;   
  193.                 thisColumn = nextColumn;   
  194.                 outputNextStringRecord = false;   
  195.             }   
  196.             break;   
  197.   
  198.         case LabelRecord.sid:   
  199.             LabelRecord lrec = (LabelRecord) record;   
  200.   
  201.             curRow = thisRow = lrec.getRow();   
  202.             thisColumn = lrec.getColumn();   
  203.             value = lrec.getValue().trim();   
  204.             value = value.equals("")?" ":value;   
  205.             this.rowlist.add(thisColumn, value);   
  206.             break;   
  207.         case LabelSSTRecord.sid:   
  208.             LabelSSTRecord lsrec = (LabelSSTRecord) record;   
  209.   
  210.             curRow = thisRow = lsrec.getRow();   
  211.             thisColumn = lsrec.getColumn();   
  212.             if (sstRecord == null) {   
  213.                 rowlist.add(thisColumn, " ");   
  214.             } else {   
  215.                 value =  sstRecord   
  216.                 .getString(lsrec.getSSTIndex()).toString().trim();   
  217.                 value = value.equals("")?" ":value;   
  218.                 rowlist.add(thisColumn,value);   
  219.             }   
  220.             break;   
  221.         case NoteRecord.sid:   
  222.             NoteRecord nrec = (NoteRecord) record;   
  223.   
  224.             thisRow = nrec.getRow();   
  225.             thisColumn = nrec.getColumn();   
  226.             // TODO: Find object to match nrec.getShapeId()   
  227.             thisStr = '"' + "(TODO)" + '"';   
  228.             break;   
  229.         case NumberRecord.sid:   
  230.             NumberRecord numrec = (NumberRecord) record;   
  231.   
  232.             curRow = thisRow = numrec.getRow();   
  233.             thisColumn = numrec.getColumn();   
  234.             value = formatListener.formatNumberDateCell(numrec).trim();   
  235.             value = value.equals("")?" ":value;   
  236.             // Format   
  237.             rowlist.add(thisColumn, value);   
  238.             break;   
  239.         case RKRecord.sid:   
  240.             RKRecord rkrec = (RKRecord) record;   
  241.   
  242.             thisRow = rkrec.getRow();   
  243.             thisColumn = rkrec.getColumn();   
  244.             thisStr = '"' + "(TODO)" + '"';   
  245.             break;   
  246.         default:   
  247.             break;   
  248.         }   
  249.   
  250.         // 遇到新行的操作   
  251.         if (thisRow != -1 && thisRow != lastRowNumber) {   
  252.             lastColumnNumber = -1;   
  253.         }   
  254.   
  255.         // 空值的操作   
  256.         if (record instanceof MissingCellDummyRecord) {   
  257.             MissingCellDummyRecord mc = (MissingCellDummyRecord) record;   
  258.             curRow = thisRow = mc.getRow();   
  259.             thisColumn = mc.getColumn();   
  260.             rowlist.add(thisColumn," ");   
  261.         }   
  262.   
  263.         // 如果遇到能打印的东西,在这里打印   
  264.         if (thisStr != null) {   
  265.             if (thisColumn > 0) {   
  266.                 output.print(',');   
  267.             }   
  268.             output.print(thisStr);   
  269.         }   
  270.   
  271.         // 更新行和列的值   
  272.         if (thisRow > -1)   
  273.             lastRowNumber = thisRow;   
  274.         if (thisColumn > -1)   
  275.             lastColumnNumber = thisColumn;   
  276.   
  277.         // 行结束时的操作   
  278.         if (record instanceof LastCellOfRowDummyRecord) {   
  279.             if (minColumns > 0) {   
  280.                 // 列值重新置空   
  281.                 if (lastColumnNumber == -1) {   
  282.                     lastColumnNumber = 0;   
  283.                 }   
  284.             }   
  285.             // 行结束时, 调用 optRows() 方法   
  286.             lastColumnNumber = -1;   
  287.             try {   
  288.                 optRows(sheetIndex,curRow, rowlist);   
  289.             } catch (SQLException e) {   
  290.                 e.printStackTrace();   
  291.             }   
  292.             rowlist.clear();   
  293.         }   
  294.     }   
  295. }  
package com.gaosheng.util.xls;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener;
import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder.SheetRecordCollectingListener;
import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;
import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord;
import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BlankRecord;
import org.apache.poi.hssf.record.BoolErrRecord;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.LabelRecord;
import org.apache.poi.hssf.record.LabelSSTRecord;
import org.apache.poi.hssf.record.NoteRecord;
import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.RKRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.SSTRecord;
import org.apache.poi.hssf.record.StringRecord;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public abstract class HxlsAbstract implements HSSFListener {
	private int minColumns;
	private POIFSFileSystem fs;
	private PrintStream output;

	private int lastRowNumber;
	private int lastColumnNumber;

	/** Should we output the formula, or the value it has? */
	private boolean outputFormulaValues = true;

	/** For parsing Formulas */
	private SheetRecordCollectingListener workbookBuildingListener;
	private HSSFWorkbook stubWorkbook;

	// Records we pick up as we process
	private SSTRecord sstRecord;
	private FormatTrackingHSSFListener formatListener;

	/** So we known which sheet we're on */
	private int sheetIndex = -1;
	private BoundSheetRecord[] orderedBSRs;
	@SuppressWarnings("unchecked")
	private ArrayList boundSheetRecords = new ArrayList();

	// For handling formulas with string results
	private int nextRow;
	private int nextColumn;
	private boolean outputNextStringRecord;

	private int curRow;
	private List<String> rowlist;
	@SuppressWarnings( "unused")
	private String sheetName;

	public HxlsAbstract(POIFSFileSystem fs)
			throws SQLException {
		this.fs = fs;
		this.output = System.out;
		this.minColumns = -1;
		this.curRow = 0;
		this.rowlist = new ArrayList<String>();
	}

	public HxlsAbstract(String filename) throws IOException,
			FileNotFoundException, SQLException {
		this(new POIFSFileSystem(new FileInputStream(filename)));
	}
	
	//excel记录行操作方法,以行索引和行元素列表为参数,对一行元素进行操作,元素为String类型
//	public abstract void optRows(int curRow, List<String> rowlist) throws SQLException ;
	
	//excel记录行操作方法,以sheet索引,行索引和行元素列表为参数,对sheet的一行元素进行操作,元素为String类型
	public abstract void optRows(int sheetIndex,int curRow, List<String> rowlist) throws SQLException;
	
	/**
	 * 遍历 excel 文件
	 */
	public void process() throws IOException {
		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);
	}
	
	/**
	 * HSSFListener 监听方法,处理 Record
	 */
	@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) record;
			if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
				// Create sub workbook if required
				if (workbookBuildingListener != null && stubWorkbook == null) {
					stubWorkbook = workbookBuildingListener
							.getStubHSSFWorkbook();
				}

				// Works by ordering the BSRs by the location of
				// their BOFRecords, and then knowing that we
				// process BOFRecords in byte offset order
				sheetIndex++;
				if (orderedBSRs == null) {
					orderedBSRs = BoundSheetRecord
							.orderByBofPosition(boundSheetRecords);
				}
				sheetName = orderedBSRs[sheetIndex].getSheetname();
			}
			break;

		case SSTRecord.sid:
			sstRecord = (SSTRecord) record;
			break;

		case BlankRecord.sid:
			BlankRecord brec = (BlankRecord) record;

			thisRow = brec.getRow();
			thisColumn = brec.getColumn();
			thisStr = "";
			break;
		case BoolErrRecord.sid:
			BoolErrRecord berec = (BoolErrRecord) record;

			thisRow = berec.getRow();
			thisColumn = berec.getColumn();
			thisStr = "";
			break;

		case FormulaRecord.sid:
			FormulaRecord frec = (FormulaRecord) record;

			thisRow = frec.getRow();
			thisColumn = frec.getColumn();

			if (outputFormulaValues) {
				if (Double.isNaN(frec.getValue())) {
					// Formula result is a string
					// This is stored in the next record
					outputNextStringRecord = true;
					nextRow = frec.getRow();
					nextColumn = frec.getColumn();
				} else {
					thisStr = formatListener.formatNumberDateCell(frec);
				}
			} else {
				thisStr = '"' + HSSFFormulaParser.toFormulaString(stubWorkbook,
						frec.getParsedExpression()) + '"';
			}
			break;
		case StringRecord.sid:
			if (outputNextStringRecord) {
				// String for formula
				StringRecord srec = (StringRecord) record;
				thisStr = srec.getString();
				thisRow = nextRow;
				thisColumn = nextColumn;
				outputNextStringRecord = false;
			}
			break;

		case LabelRecord.sid:
			LabelRecord lrec = (LabelRecord) record;

			curRow = thisRow = lrec.getRow();
			thisColumn = lrec.getColumn();
			value = lrec.getValue().trim();
			value = value.equals("")?" ":value;
			this.rowlist.add(thisColumn, value);
			break;
		case LabelSSTRecord.sid:
			LabelSSTRecord lsrec = (LabelSSTRecord) record;

			curRow = thisRow = lsrec.getRow();
			thisColumn = lsrec.getColumn();
			if (sstRecord == null) {
				rowlist.add(thisColumn, " ");
			} else {
				value =  sstRecord
				.getString(lsrec.getSSTIndex()).toString().trim();
				value = value.equals("")?" ":value;
				rowlist.add(thisColumn,value);
			}
			break;
		case NoteRecord.sid:
			NoteRecord nrec = (NoteRecord) record;

			thisRow = nrec.getRow();
			thisColumn = nrec.getColumn();
			// TODO: Find object to match nrec.getShapeId()
			thisStr = '"' + "(TODO)" + '"';
			break;
		case NumberRecord.sid:
			NumberRecord numrec = (NumberRecord) record;

			curRow = thisRow = numrec.getRow();
			thisColumn = numrec.getColumn();
			value = formatListener.formatNumberDateCell(numrec).trim();
			value = value.equals("")?" ":value;
			// Format
			rowlist.add(thisColumn, value);
			break;
		case RKRecord.sid:
			RKRecord rkrec = (RKRecord) record;

			thisRow = rkrec.getRow();
			thisColumn = rkrec.getColumn();
			thisStr = '"' + "(TODO)" + '"';
			break;
		default:
			break;
		}

		// 遇到新行的操作
		if (thisRow != -1 && thisRow != lastRowNumber) {
			lastColumnNumber = -1;
		}

		// 空值的操作
		if (record instanceof MissingCellDummyRecord) {
			MissingCellDummyRecord mc = (MissingCellDummyRecord) record;
			curRow = thisRow = mc.getRow();
			thisColumn = mc.getColumn();
			rowlist.add(thisColumn," ");
		}

		// 如果遇到能打印的东西,在这里打印
		if (thisStr != null) {
			if (thisColumn > 0) {
				output.print(',');
			}
			output.print(thisStr);
		}

		// 更新行和列的值
		if (thisRow > -1)
			lastRowNumber = thisRow;
		if (thisColumn > -1)
			lastColumnNumber = thisColumn;

		// 行结束时的操作
		if (record instanceof LastCellOfRowDummyRecord) {
			if (minColumns > 0) {
				// 列值重新置空
				if (lastColumnNumber == -1) {
					lastColumnNumber = 0;
				}
			}
			// 行结束时, 调用 optRows() 方法
			lastColumnNumber = -1;
			try {
				optRows(sheetIndex,curRow, rowlist);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rowlist.clear();
		}
	}
}

 

继承类: HxlsBig,作用:将excel中数据转储到数据库临时表中,实现optRows方法

 

Java代码 复制代码
  1. package com.gaosheng.util.examples.xls;   
  2. import java.io.FileInputStream;   
  3. import java.io.FileNotFoundException;   
  4. import java.io.IOException;   
  5. import java.io.PrintStream;   
  6. import java.sql.Connection;   
  7. import java.sql.DriverManager;   
  8. import java.sql.PreparedStatement;   
  9. import java.sql.SQLException;   
  10. import java.sql.Statement;   
  11. import java.util.List;   
  12. import java.util.Properties;   
  13.   
  14. import org.apache.poi.poifs.filesystem.POIFSFileSystem;   
  15.   
  16. import com.gaosheng.util.xls.HxlsAbstract;   
  17.   
  18.   
  19. public class HxlsBig extends HxlsAbstract{   
  20.        
  21.     public static void main(String[] args) throws Exception {   
  22.         // XLS2CSVmra xls2csv = new XLS2CSVmra(args[0], minColumns);   
  23.         HxlsBig xls2csv = new HxlsBig("E:/up.xls","hxls_temp");   
  24.         xls2csv.process();   
  25.         xls2csv.close();   
  26.     }   
  27.        
  28.     public HxlsBig(POIFSFileSystem fs, PrintStream output,String tableName)   
  29.             throws SQLException {   
  30.         super(fs);   
  31.         this.conn = getNew_Conn();   
  32.         this.statement = conn.createStatement();   
  33.         this.tableName = tableName;   
  34.     }   
  35.   
  36.     public HxlsBig(String filename,String tableName) throws IOException,   
  37.             FileNotFoundException, SQLException {   
  38.         this(new POIFSFileSystem(new FileInputStream(filename)), System.out,tableName);   
  39.     }   
  40.   
  41.     private Connection conn = null;   
  42.     private Statement statement = null;   
  43.     private PreparedStatement newStatement = null;   
  44.   
  45.     private String tableName = "temp_table";   
  46.     private boolean create = true;   
  47. //  private int sheetIndex = 0;   
  48.        
  49.     public void optRows(int sheetIndex,int curRow, List<String> rowlist) throws SQLException {   
  50.         if (curRow == 0 && sheetIndex == 0 ) {   
  51.             StringBuffer preSql = new StringBuffer("insert into " + tableName   
  52.                     + " values(");   
  53.             StringBuffer table = new StringBuffer("create table " + tableName   
  54.                     + "(");   
  55.             int c = rowlist.size();   
  56.             for (int i = 0; i < c; i++) {   
  57.                 preSql.append("?,");   
  58.                 table.append(rowlist.get(i));   
  59.                 table.append("  varchar2(100) ,");   
  60.             }   
  61.   
  62.             table.deleteCharAt(table.length() - 1);   
  63.             preSql.deleteCharAt(preSql.length() - 1);   
  64.             table.append(")");   
  65.             preSql.append(")");   
  66.             if (create) {   
  67.                 statement = conn.createStatement();   
  68.                 try{   
  69.                     statement.execute("drop table "+tableName);   
  70.                 }catch(Exception e){   
  71.                        
  72.                 }finally{   
  73.                     System.out.println("表 "+tableName+" 删除成功");   
  74.                 }   
</d
  • src.zip (11.2 KB)
  • 下载次数: 19
分享到:
评论

你可能感兴趣的:(apache,sql,Excel,UP)