Jxl crear operaciones básicas usando Excel

  En el mundo del código abierto, hay dos API más influyente disponibles, uno es el punto de interés, es una jExcelAPI. En donde la función jxl es relativamente débil punto de PDI. Pero jExcelAPI para el apoyo chino es muy buena, es una API de Java puro, no se basa en los sistemas Windows, incluso mientras se ejecuta en Linux, también es capaz de manejar correctamente los archivos de Excel. También es necesario tener en cuenta es que este API es un apoyo muy limitado para los gráficos y tablas, pero sólo identificar el formato PNG.

entorno construido: el archivo después de la descarga de desempaquetado, obtener jxl.jar, en la ruta de clase, la instalación se ha completado.

Descargar el paquete jar:

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

Funcionamiento básico:

1. Crear Excel y datos de escritura



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("创建完毕");
		
		
	}
	
	
}

Aplicación de los resultados:

2. Leer archivo de Excel:

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();
		}
		
	}
}

Los resultados:

Publicado 50 artículos originales · ganado elogios 21 · vistas 3705

Supongo que te gusta

Origin blog.csdn.net/wmlwml0000/article/details/80842772
Recomendado
Clasificación