首页 > 代码库 > java导入excel
java导入excel
package com.duosen.gate.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelReader {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ExcelReader.readExcel(new File("D:/a.xls"));
ExcelReader.getExcel();
}
//private static ExcelInterface excelInterface = null;
private static Workbook wb = null;
//读取excel中的某一个 sheet.
public static void readExcel(File excelFile){
try {
FileInputStream is = new FileInputStream(excelFile);
//读取excel数据表的第一步是创建workbook工作簿
wb = Workbook.getWorkbook(is);
//通过 workbook来 访问excel sheet
Sheet sh1 = wb.getSheet(0);
//通过 sheet访问excel cell,获取第一行第一列 的值
// Cell c1 = sh1.getCell(1, 0);
//获得excel所有sheet的名称
/*String sheetNames[] = wb.getSheetNames();
//读取一个excel里的所有sheet名称
for (int i = 0; i < sheetNames.length; i++) {
System.out.println("sheet name="+ i +sheetNames[i]);
}*/
int columns = sh1.getColumns();
for (int i = 0; i < sh1.getRows(); i++) {
//String[] nextLine = new String[columns];
System.out.println();
for (int j = 0; j < columns; j++) {
//注意任何一个cell(单元格)getContents()以后都会得到一个字符串(不管它原来是什么类型,eg:整形、浮点型、字符....)
String cellVal = sh1.getCell(j, i).getContents();
//System.out.print(sh1.getCell(j, i).getContents()+" || ");
System.out.println(cellVal+" || ");
}
}
//关闭
wb.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void getExcel(){
try {
File f = new File("d:/a.xls");
WritableWorkbook workbook = Workbook.createWorkbook(f);
WritableSheet sheet = workbook.createSheet("1", 0);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 1000; j++) {
Label label = new Label(i,j,"sdss"+i+"-"+j);
sheet.addCell(label);
}
}
workbook.write();
workbook.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelReader {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ExcelReader.readExcel(new File("D:/a.xls"));
ExcelReader.getExcel();
}
//private static ExcelInterface excelInterface = null;
private static Workbook wb = null;
//读取excel中的某一个 sheet.
public static void readExcel(File excelFile){
try {
FileInputStream is = new FileInputStream(excelFile);
//读取excel数据表的第一步是创建workbook工作簿
wb = Workbook.getWorkbook(is);
//通过 workbook来 访问excel sheet
Sheet sh1 = wb.getSheet(0);
//通过 sheet访问excel cell,获取第一行第一列 的值
// Cell c1 = sh1.getCell(1, 0);
//获得excel所有sheet的名称
/*String sheetNames[] = wb.getSheetNames();
//读取一个excel里的所有sheet名称
for (int i = 0; i < sheetNames.length; i++) {
System.out.println("sheet name="+ i +sheetNames[i]);
}*/
int columns = sh1.getColumns();
for (int i = 0; i < sh1.getRows(); i++) {
//String[] nextLine = new String[columns];
System.out.println();
for (int j = 0; j < columns; j++) {
//注意任何一个cell(单元格)getContents()以后都会得到一个字符串(不管它原来是什么类型,eg:整形、浮点型、字符....)
String cellVal = sh1.getCell(j, i).getContents();
//System.out.print(sh1.getCell(j, i).getContents()+" || ");
System.out.println(cellVal+" || ");
}
}
//关闭
wb.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void getExcel(){
try {
File f = new File("d:/a.xls");
WritableWorkbook workbook = Workbook.createWorkbook(f);
WritableSheet sheet = workbook.createSheet("1", 0);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 1000; j++) {
Label label = new Label(i,j,"sdss"+i+"-"+j);
sheet.addCell(label);
}
}
workbook.write();
workbook.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。