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
}
}
}
相关文章
- 02-23Java程序内存分析:使用mat工具分析内存占用
- 02-23JAVA内存泄露查询
- 02-23java内存溢出(OutOfMemoryError)问题排查
- 02-23Android-Java-静态变量与静态方法内存图
- 02-23Excel-计算年龄、工龄 datedif()
- 02-232019.06.05 ABAP EXCEL 操作类代码 OLE方式(模板下载,excel上传,内表下载)
- 02-23解决C#使用Microsoft.Office.Interop.Excel操作Excel后进程一直存在的问题
- 02-23重写hashCode和equals实现导入员工数据
- 02-23用3dMax给lol人物模型制作表情动画并导入Unity
- 02-23.net利用NPOI生成excel文件