Jxl create basic operations using excel

  In the open source world, there are two more influential API available, one is the POI, is a jExcelAPI. Wherein jxl function is relatively weak POI point. But jExcelAPI for Chinese support is very good, API is a pure Java, does not rely on Windows systems, even while running under Linux, it is also able to correctly handling Excel files. Also need to note it is that this API is very limited support for graphics and charts, but only identify the PNG format.

Built environment: the file after downloading unpack, get jxl.jar, into the classpath, the installation is complete.

Download jar package:

https://download.csdn.net/download/wmlwml0000/10505517

Basic operation:

1. Create excel, and write data



import java.io.File;

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

/**
 * 
 * ClassName: CreateExcel <br/>
 * Function:创建excel <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2018年6月28日 下午2:26:27 <br/>
 * @author Soulmate.leilei
 * @version 
 * @since JDK 1.8
 */
public class CreateExcel {

	public static void main(String[] args) {
		
		try {
			//创建Excel文件
			File file =new File("C:\\Users\\Administrator\\Desktop\\testfile.xls");
			file.createNewFile();
			//创建工作簿,然后进行
			WritableWorkbook workbook =Workbook.createWorkbook(file);
			//创建sheet
			WritableSheet sheet =workbook.createSheet("Sheet" , 0);
			WritableSheet sheet2=workbook.createSheet("sheet2", 1);
			//第一行设置列名:创建数组
			String[] title={"编号","姓名","性别","年龄"};
			Label label =null ;
			//设置列名
			for (int i = 0; i < title.length; i++) {
				label=new Label(i,0,title[i]);
				sheet.addCell(label);
			}
			//追加数据
			for (int i = 1; i <=30; i++) {
				label=new Label(0,i,"id"+i);
				sheet.addCell(label);
				label= new Label(1,i,"张"+i);
				sheet.addCell(label);
				label=new Label(2,i,"男");
				sheet.addCell(label);
				label=new Label(3,i,"18"+i);
				sheet.addCell(label);
				
			}
			//写入数据
			workbook.write();
			workbook.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("创建完毕");
		
		
	}
	
	
}

Implementation of the results:

2. Read Excel file:

import java.io.File;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

/**
 * ClassName: ReadExcel <br/>
 * Function: 读取Excel. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2018年6月28日 下午2:35:18 <br/>
 * @author Soulmate.leilei
 * @version 
 * @since JDK 1.8
 */
public class ReadExcel {

	public static void main(String[] args) {
		
		try {
			//获取文件
			File file = new File("C:\\Users\\Administrator\\Desktop\\testfile.xls");
			//创建WorkBook
			Workbook workbook = Workbook.getWorkbook(file);
			//获取第一个工作表sheet
			Sheet sheet = workbook.getSheet(0);
		
			//获取数据
			for(int i =1;i<sheet.getRows();i++){
				for(int j=0;j<sheet.getColumns();j++){
					Cell cell = sheet.getCell(j,i);
					System.out.print(cell.getContents()+" ");
					
				}
				System.out.println();
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

Results of the:

Published 50 original articles · won praise 21 · views 3705

Guess you like

Origin blog.csdn.net/wmlwml0000/article/details/80842772
jxl