EXCEL导入Java代码

来源:互联网 发布:淘宝天机平台不让申请 编辑:程序博客网 时间:2024/05/18 22:44
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;


/**
 * 
 */


/**
 * @author Ethan
 * 
 * @date 2013-9-26
 */
public class ExcelParse {
public static void main(String[] args) {
int i;
Sheet sheet;
Workbook book;
Cell cell1, cell2, cell3;
try {
File file=new File("D:\\12.xls");
book = Workbook.getWorkbook(file);
sheet = book.getSheet(0);
cell1 = sheet.getCell(0, 0);
System.out.println("标题:" + cell1.getContents());
i = 1;
while (true) {
// 获取每一行的单元格
cell1 = sheet.getCell(0, i);// (列,行)
cell2 = sheet.getCell(1, i);
cell3 = sheet.getCell(2, i);
if ("".equals(cell1.getContents()) == true) // 如果读取的数据为空
break;
System.out.println(cell1.getContents() + "\t"
+ cell2.getContents() + "\t" + cell3.getContents());
i++;
}
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// public static void main(String[] args) throws IOException {
// new ExcelParse().readXls();
// }


private void readXls() throws IOException {
InputStream is = new FileInputStream("D:\\12.xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);


// 循环工作表Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}


// 循环行Row
for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}


// 循环列Cell
for (int cellNum = 0; cellNum <= hssfRow.getLastCellNum(); cellNum++) {
HSSFCell hssfCell = hssfRow.getCell(cellNum);
if (hssfCell == null) {
continue;
}


System.out.print("    " + getValue(hssfCell));
}
System.out.println();
}
}
}


@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
return String.valueOf(hssfCell.getNumericCellValue());
} else {
return String.valueOf(hssfCell.getStringCellValue());
}

}


独立改造

public static void main(String[] args) throws IOException {
new ExcelParse().readXls();
}


private void readXls() {
InputStream is = null;
String excelPath="D:\\12.xls";
String[] columnName={"class","name","score"};
File f = new File("D:\\12.txt");
FileWriter fw = null;
BufferedWriter bw = null;
try {
is = new FileInputStream(excelPath);
fw = new FileWriter(f);
bw = new BufferedWriter(fw);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {// 循环工作表Sheet
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {// 循环行Row
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}
bw.write("UPDATE TABLE STUDENT SET " + columnName[2] + "="
+ getValue(hssfRow.getCell(2)) + " WHERE "
+ columnName[0] + "='"
+ getValue(hssfRow.getCell(0)) + "' AND "
+ columnName[1] + "='"
+ getValue(hssfRow.getCell(1)) + "';\r\n");
// bw.newLine();
// if(rowNum)
bw.flush();
}
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


@SuppressWarnings({ "static-access", "deprecation" })
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
return String.valueOf(hssfCell.getNumericCellValue());
} else {
return hssfCell.getStringCellValue();
}
}