码迷,mamicode.com
首页 > 其他好文 > 详细

2321

时间:2015-06-02 20:19:50      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:2321

package util;


import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.text.DateFormat;

import java.text.DecimalFormat;


import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;


import database.DBMSDataInfo2;


public class ExcelReaderUtils {

private BufferedReader reader = null;

private String filetype;

private InputStream is = null;

private int currSheet = 0;

private int currPosition = 0;

private int sheetCount;

private Workbook workbook = null;

public static String EXCEL_LINE_DELIMITER = "#@#";


public ExcelReaderUtils(String inputfile, int currSheet)

throws Exception {

this(inputfile);

this.currSheet = currSheet;

}


public ExcelReaderUtils(String inputfile, int currSheet, int currPosition)

throws Exception {

this(inputfile);

this.currSheet = currSheet;

this.currPosition = currPosition;

}


public ExcelReaderUtils(String inputfile) throws Exception {

if ((inputfile == null) || (inputfile.trim().equals(""))) {

throw new IOException("no input file specified");

}

this.filetype = inputfile.substring(inputfile.lastIndexOf(".") + 1);


this.currPosition = 0;


this.is = new FileInputStream(inputfile);

if (this.filetype.equalsIgnoreCase("txt")) {

this.reader = new BufferedReader(new InputStreamReader(this.is));

} else if (this.filetype.equalsIgnoreCase("xls")) {

this.workbook = new HSSFWorkbook(this.is);

this.sheetCount = this.workbook.getNumberOfSheets();

} else if (this.filetype.equalsIgnoreCase("xlsx")) {

this.workbook = new XSSFWorkbook(this.is);

} else {

throw new Exception("File Type Not Supported");

}

}

public ExcelReaderUtils(InputStream is,String filetype) throws Exception {

this.filetype = filetype;

this.currPosition = 0;

this.is = is;

if (this.filetype.equalsIgnoreCase("txt")) {

this.reader = new BufferedReader(new InputStreamReader(this.is));

} else if (this.filetype.equalsIgnoreCase("xls")) {

this.workbook = new HSSFWorkbook(this.is);

this.sheetCount = this.workbook.getNumberOfSheets();

} else if (this.filetype.equalsIgnoreCase("xlsx")) {

this.workbook = new XSSFWorkbook(this.is);

} else {

throw new Exception("File Type Not Supported");

}

}

public ExcelReaderUtils(InputStream is,String filetype,int currSheet) throws Exception {

this.filetype = filetype;

this.currPosition = 0;

this.currSheet = currSheet;

this.is = is;

if (this.filetype.equalsIgnoreCase("txt")) {

this.reader = new BufferedReader(new InputStreamReader(this.is));

} else if (this.filetype.equalsIgnoreCase("xls")) {

this.workbook = new HSSFWorkbook(this.is);

this.sheetCount = this.workbook.getNumberOfSheets();

} else if (this.filetype.equalsIgnoreCase("xlsx")) {

this.workbook = new XSSFWorkbook(this.is);

} else {

throw new Exception("File Type Not Supported");

}

}


public ExcelReaderUtils(InputStream is,String filetype,int currSheet,int currPosition) throws Exception {

this.filetype = filetype;

this.currPosition = currPosition;

this.currSheet = currSheet;

this.is = is;

if (this.filetype.equalsIgnoreCase("txt")) {

this.reader = new BufferedReader(new InputStreamReader(this.is));

} else if (this.filetype.equalsIgnoreCase("xls")) {

this.workbook = new HSSFWorkbook(this.is);

this.sheetCount = this.workbook.getNumberOfSheets();

} else if (this.filetype.equalsIgnoreCase("xlsx")) {

this.workbook = new XSSFWorkbook(this.is);

} else {

throw new Exception("File Type Not Supported");

}

}


public boolean haxNextLine() throws IOException {

if (this.filetype.equalsIgnoreCase("txt")) {

return false;

}

Sheet sheet = this.workbook.getSheetAt(this.currSheet);

if ((this.currPosition > sheet.getLastRowNum())

&& (this.currSheet != this.sheetCount - 1)) {

return true;

}

if (this.currPosition <= sheet.getLastRowNum()) {

return true;

}

return false;

}


public String readLine() throws IOException {

if (this.filetype.equalsIgnoreCase("txt")) {

String str = this.reader.readLine();

while (str.trim().equals("")) {

str = this.reader.readLine();

}

return str;

}

if (this.filetype.equalsIgnoreCase("xls") || this.filetype.equalsIgnoreCase("xlsx")) {

Sheet sheet = this.workbook.getSheetAt(this.currSheet);

if (this.currPosition > sheet.getLastRowNum()) {

this.currPosition = 0;

while (this.currSheet != this.sheetCount - 1) {

this.currSheet += 1;

sheet = this.workbook.getSheetAt(this.currSheet);

if (this.currPosition != sheet.getLastRowNum()) {

int row = this.currPosition;

this.currPosition += 1;

return getLine(sheet, row);

}

}

return null;

}

int row = this.currPosition;

this.currPosition += 1;

return getLine(sheet, row);

}

return null;

}


public String[] readAllLines() {

return getAllLines(this.currSheet);

}


public String[] readAllLines(int currSheet) {

return getAllLines(currSheet);

}


private String getLine(Sheet sheet, int row) {

Row rowLine = sheet.getRow(row);

return getAllCells(rowLine);

}


private String[] getAllLines(int currSheet) {

Sheet sheet = this.workbook.getSheetAt(currSheet);


String[] strs = new String[sheet.getLastRowNum() + 1];

for (int i = 0; i <= sheet.getLastRowNum(); i++) {

Row rowLine = sheet.getRow(i);

strs[i] = getAllCells(rowLine);

}

return strs;

}


private String getAllCells(Row rowLine) {

StringBuffer buffer = new StringBuffer();

if (rowLine == null) {

return "";

}

int cellCount = rowLine.getLastCellNum();

for (int j = 0; j < cellCount; j++) {

Cell cell = rowLine.getCell(j);

if (cell != null) {

switch (cell.getCellType()) {

case HSSFCell.CELL_TYPE_NUMERIC:

if (HSSFDateUtil.isCellDateFormatted(cell)) {

DateFormat df = DateFormat.getDateInstance();

buffer.append(df.format(cell.getDateCellValue()));

} else {

double cellValue = cell.getNumericCellValue();

buffer.append(new DecimalFormat("#.######").format(cellValue)); 

}

break;

case HSSFCell.CELL_TYPE_STRING:

buffer.append(cell.getRichStringCellValue().toString()

.replaceAll("‘", "‘‘"));

break;

case HSSFCell.CELL_TYPE_FORMULA:

buffer.append(cell.getNumericCellValue());

break;

default:

buffer.append(" ");

break;

}

} else {

buffer.append(" ");

}

buffer.append(EXCEL_LINE_DELIMITER);

}

return buffer.toString();

}


public void setCurrPosition(int currPosition) {

this.currPosition = currPosition;

}


public int getSheetCount() {

return this.sheetCount;

}


public void close() {

if (this.is != null) {

try {

this.is.close();

} catch (IOException e) {

this.is = null;

}

}

if (this.reader != null) {

try {

this.reader.close();

} catch (IOException e) {

this.reader = null;

}

}

}


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

ExcelReaderUtils eru = new ExcelReaderUtils("D:\\wangwei\\test.xlsx");

for(String str:eru.readAllLines(0)){

System.out.println(str);

};

}

private static void readDML() throws Exception{

ExcelReaderUtils eru = new ExcelReaderUtils("D:\\SQL\\DDL_DML.xlsx");

StringBuffer sb = new StringBuffer();

for(String str:eru.readAllLines(0)){

sb.append(str.replace(EXCEL_LINE_DELIMITER, ","));

};

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

String content = sb.toString().substring(0,sb.toString().lastIndexOf(","));

System.out.println(content);

System.out.println("---------ddl--"+content.split(",").length+"--------");

System.err.println("---------ddl--"+DBMSDataInfo2.ddlTable.split(",").length+"--------");

System.out.println();

sb.delete(0, sb.toString().length());

for(String str:eru.readAllLines(1)){

sb.append(str.replace(EXCEL_LINE_DELIMITER, ","));

};

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

content = sb.toString().substring(0,sb.toString().lastIndexOf(","));

System.out.println(content);

System.out.println("---------dml--"+content.split(",").length+"--------");

System.out.println();

sb = new StringBuffer();

for(String str:eru.readAllLines(2)){

sb.append(str.replace(EXCEL_LINE_DELIMITER, ","));

};

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

content = sb.toString().substring(0,sb.toString().lastIndexOf(","));

System.out.println(content);

System.out.println("---------data--"+content.split(",").length+"--------");

}

}


2321

标签:2321

原文地址:http://4233497.blog.51cto.com/4223497/1657597

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!