java 创建和读取Excel表单

时间:2021-10-22 05:39:13
src源码如下  引用的jxl包地址-->http://pan.baidu.com/s/1gd0zcbD
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class Excel {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//writeExcel();
		readExcel();

	}
	
	static void writeExcel(){
		try {
			WritableWorkbook book=Workbook.createWorkbook(new File("d:/1.xls"));
			WritableSheet sheet1=book.createSheet("first page", 0);
			WritableSheet sheet2=book.createSheet("second page", 1);
			for (int i = 0; i < 10; i++) {
				for (int j = 0; j < 10; j++) {
					Label lb1=new Label(i, j, "("+i+","+j+")");
					sheet1.addCell(lb1);
				}
				
			}
			for (int i = 0; i < 10; i++) {
				for (int j = 0; j < 10; j++) {
					Label lb2=new Label(i, j, "("+i+","+j+")");
					sheet2.addCell(lb2);
				}
				
			}
			//write the data and close the file
			book.write();
			book.close();
			System.out.println("the excel fil hava been wrotten");
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
    static void readExcel(){
    	try {
    		InputStream is=new FileInputStream("d:\\1.xls");
    		Workbook wb=Workbook.getWorkbook(is);
    		jxl.Sheet  []sheet=wb.getSheets();
    		for (int i = 0; i < sheet.length; i++) {
				for (int j = 0; j < sheet[i].getRows(); j++) {
					for (int j2 = 0; j2 < sheet[i].getColumns(); j2++) {
						System.out.print(sheet[i].getCell(j, j2).getContents());
					}
					System.out.println();
				}
				System.out.println();
			}
    		wb.close();
    		
		} catch (Exception e) {
			// TODO: handle exception
		}
    	
    }
}




java 创建和读取Excel表单