POI操作Excel详解,HSSF和XSSF两种方式

使用 easypoi 导出带三级下拉联动的 excel 阅读详情

HSSF方式:

package com.tools.poi.lesson1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;

import com.tools.poi.bean.Student;

public class ExcelUtilWithHSSF {
	public static void main(String[] args) {
		try {
			getExcelAsFile("aaa");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
//		try {
//			CreateExcelDemo1();
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
		
		
	}
	
	/**
	 * 得到Excel,并解析内容
	 * @param file
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void getExcelAsFile(String file) throws FileNotFoundException, IOException{
		//1.得到Excel常用对象
//		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/test.xls"));
		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/new1.xls"));
		//2.得到Excel工作簿对象
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		//3.得到Excel工作表对象
		HSSFSheet sheet = wb.getSheetAt(0);
		//总行数
		int trLength = sheet.getLastRowNum();
		//4.得到Excel工作表的行
		HSSFRow row = sheet.getRow(0);
		//总列数
		int tdLength = row.getLastCellNum();
		//5.得到Excel工作表指定行的单元格
		HSSFCell cell = row.getCell((short)1);
		//6.得到单元格样式
		CellStyle cellStyle = cell.getCellStyle();
		for(int i=0;i<trLength;i++){
			//得到Excel工作表的行
			HSSFRow row1 = sheet.getRow(i);
			for(int j=0;j<tdLength;j++){
				
			//得到Excel工作表指定行的单元格
			HSSFCell cell1 = row1.getCell(j);
			
			/**
			 * 为了处理:Excel异常Cannot get a text value from a numeric cell
			 * 将所有列中的内容都设置成String类型格式
			 */
			if(cell1!=null){
		          cell1.setCellType(Cell.CELL_TYPE_STRING);
		     }
			
			//获得每一列中的值
			System.out.print(cell1.getStringCellValue()+"\t\t\t");
			}
			System.out.println();
		}
	}
	
	
	/**
	 * 创建Excel,并写入内容
	 */
	public static void CreateExcel(){
		
		//1.创建Excel工作薄对象
		HSSFWorkbook wb = new HSSFWorkbook();
		//2.创建Excel工作表对象     
		HSSFSheet sheet = wb.createSheet("new Sheet");
		//3.创建Excel工作表的行   
		HSSFRow row = sheet.createRow(6);
		//4.创建单元格样式
		CellStyle cellStyle =wb.createCellStyle();
	      // 设置这些样式
		cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	      
	      
	      
		//5.创建Excel工作表指定行的单元格
		row.createCell(0).setCellStyle(cellStyle);
		//6.设置Excel工作表的值
		row.createCell(0).setCellValue("aaaa");
		
		row.createCell(1).setCellStyle(cellStyle);
		row.createCell(1).setCellValue("bbbb");
		
		
		//设置sheet名称和单元格内容
		wb.setSheetName(0,"第一张工作表");
		//设置单元格内容	cell.setCellValue("单元格内容");
		
		// 最后一步,将文件存到指定位置
				try
				{
					FileOutputStream fout = new FileOutputStream("E:/students.xls");
					wb.write(fout);
					fout.close();
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
	}
	
	/**
	 * 创建Excel的实例
	 * @throws ParseException 
	 */
	public static void CreateExcelDemo1() throws ParseException{
		List list = new ArrayList();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
		Student user1 = new Student(1, "张三", 16,true, df.parse("1997-03-12"));
		Student user2 = new Student(2, "李四", 17,true, df.parse("1996-08-12"));
		Student user3 = new Student(3, "王五", 26,false, df.parse("1985-11-12"));
		list.add(user1);
		list.add(user2);
		list.add(user3);
		
		
		// 第一步,创建一个webbook,对应一个Excel文件
				HSSFWorkbook wb = new HSSFWorkbook();
				// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
				HSSFSheet sheet = wb.createSheet("学生表一");
				// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
				HSSFRow row = sheet.createRow((int) 0);
				// 第四步,创建单元格,并设置值表头 设置表头居中
				HSSFCellStyle style = wb.createCellStyle();
				style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

				HSSFCell cell = row.createCell((short) 0);
				cell.setCellValue("学号");
				cell.setCellStyle(style);
				cell = row.createCell((short) 1);
				cell.setCellValue("姓名");
				cell.setCellStyle(style);
				cell = row.createCell((short) 2);
				cell.setCellValue("年龄");
				cell.setCellStyle(style);
				cell = row.createCell((short) 3);
				cell.setCellValue("性别");
				cell.setCellStyle(style);
				cell = row.createCell((short) 4);
				cell.setCellValue("生日");
				cell.setCellStyle(style);

				// 第五步,写入实体数据 实际应用中这些数据从数据库得到,

				for (int i = 0; i < list.size(); i++)
				{
					row = sheet.createRow((int) i + 1);
					Student stu = (Student) list.get(i);
					// 第四步,创建单元格,并设置值
					row.createCell((short) 0).setCellValue((double) stu.getId());
					row.createCell((short) 1).setCellValue(stu.getName());
					row.createCell((short) 2).setCellValue((double) stu.getAge());
					row.createCell((short)3).setCellValue(stu.getSex()==true?"男":"女");
					cell = row.createCell((short) 4);
					cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu
							.getBirthday()));
				}
				// 第六步,将文件存到指定位置
				try
				{
					FileOutputStream fout = new FileOutputStream("E:/students.xls");
					wb.write(fout);
					fout.close();
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
		
		
		
	}
}

XSSF方式:

package com.tools.poi.lesson1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.WorkbookUtil;

import com.tools.poi.bean.Student;

public class ExcelUtilWithXSSF {
	public static void main(String[] args) {
		try {
			getExcelAsFile("d:/FTP/系统报表.xls");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		}
		
		
//		try {
//			CreateExcelDemo1();
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
		
		
	}
	
	/**
	 * 得到Excel,并解析内容  对2007及以上版本 使用XSSF解析
	 * @param file
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @throws InvalidFormatException 
	 */
	public static void getExcelAsFile(String file) throws FileNotFoundException, IOException, InvalidFormatException{
//		//1.得到Excel常用对象
//		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/FTP/new1.xls"));
//		//2.得到Excel工作簿对象
//		HSSFWorkbook wb = new HSSFWorkbook(fs);

		
		
		InputStream ins = null;   
        Workbook wb = null;   
            ins=new FileInputStream(new File(file));   
            //ins= ExcelService.class.getClassLoader().getResourceAsStream(filePath);   
            wb = WorkbookFactory.create(ins);   
            ins.close();   
		
		
		//3.得到Excel工作表对象
		Sheet sheet = wb.getSheetAt(0);
		//总行数
		int trLength = sheet.getLastRowNum();
		//4.得到Excel工作表的行
		Row row = sheet.getRow(0);
		//总列数
		int tdLength = row.getLastCellNum();
		//5.得到Excel工作表指定行的单元格
		Cell cell = row.getCell((short)1);
		//6.得到单元格样式
		CellStyle cellStyle = cell.getCellStyle();

		for(int i=5;i<trLength;i++){
			//得到Excel工作表的行
			Row row1 = sheet.getRow(i);
			for(int j=0;j<tdLength;j++){
			//得到Excel工作表指定行的单元格
			Cell cell1 = row1.getCell(j);
			/**
			 * 为了处理:Excel异常Cannot get a text value from a numeric cell
			 * 将所有列中的内容都设置成String类型格式
			 */
			if(cell1!=null){
		          cell1.setCellType(Cell.CELL_TYPE_STRING);
		     }
			
			if(j==5&&i<=10){
				cell1.setCellValue("1000");
			}
			
			//获得每一列中的值
			System.out.print(cell1+"                   ");
			}
			System.out.println();
		}
		
		//将修改后的数据保存
		OutputStream out = new FileOutputStream(file);
                wb.write(out);
	}
	
	
	/**
	 * 创建Excel,并写入内容
	 */
	public static void CreateExcel(){
		
		//1.创建Excel工作薄对象
		HSSFWorkbook wb = new HSSFWorkbook();
		//2.创建Excel工作表对象     
		HSSFSheet sheet = wb.createSheet("new Sheet");
		//3.创建Excel工作表的行   
		HSSFRow row = sheet.createRow(6);
		//4.创建单元格样式
		CellStyle cellStyle =wb.createCellStyle();
	      // 设置这些样式
		cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	      
	      
	      
		//5.创建Excel工作表指定行的单元格
		row.createCell(0).setCellStyle(cellStyle);
		//6.设置Excel工作表的值
		row.createCell(0).setCellValue("aaaa");
		
		row.createCell(1).setCellStyle(cellStyle);
		row.createCell(1).setCellValue("bbbb");
		
		
		//设置sheet名称和单元格内容
		wb.setSheetName(0,"第一张工作表");
		//设置单元格内容	cell.setCellValue("单元格内容");
		
		// 最后一步,将文件存到指定位置
				try
				{
					FileOutputStream fout = new FileOutputStream("E:/students.xls");
					wb.write(fout);
					fout.close();
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
	}
	
	/**
	 * 创建Excel的实例
	 * @throws ParseException 
	 */
	public static void CreateExcelDemo1() throws ParseException{
		List list = new ArrayList();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
		Student user1 = new Student(1, "张三", 16,true, df.parse("1997-03-12"));
		Student user2 = new Student(2, "李四", 17,true, df.parse("1996-08-12"));
		Student user3 = new Student(3, "王五", 26,false, df.parse("1985-11-12"));
		list.add(user1);
		list.add(user2);
		list.add(user3);
		
		
		// 第一步,创建一个webbook,对应一个Excel文件
				HSSFWorkbook wb = new HSSFWorkbook();
				// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
				HSSFSheet sheet = wb.createSheet("学生表一");
				// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
				HSSFRow row = sheet.createRow((int) 0);
				// 第四步,创建单元格,并设置值表头 设置表头居中
				HSSFCellStyle style = wb.createCellStyle();
				style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

				HSSFCell cell = row.createCell((short) 0);
				cell.setCellValue("学号");
				cell.setCellStyle(style);
				cell = row.createCell((short) 1);
				cell.setCellValue("姓名");
				cell.setCellStyle(style);
				cell = row.createCell((short) 2);
				cell.setCellValue("年龄");
				cell.setCellStyle(style);
				cell = row.createCell((short) 3);
				cell.setCellValue("性别");
				cell.setCellStyle(style);
				cell = row.createCell((short) 4);
				cell.setCellValue("生日");
				cell.setCellStyle(style);

				// 第五步,写入实体数据 实际应用中这些数据从数据库得到,

				for (int i = 0; i < list.size(); i++)
				{
					row = sheet.createRow((int) i + 1);
					Student stu = (Student) list.get(i);
					// 第四步,创建单元格,并设置值
					row.createCell((short) 0).setCellValue((double) stu.getId());
					row.createCell((short) 1).setCellValue(stu.getName());
					row.createCell((short) 2).setCellValue((double) stu.getAge());
					row.createCell((short)3).setCellValue(stu.getSex()==true?"男":"女");
					cell = row.createCell((short) 4);
					cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu
							.getBirthday()));
				}
				// 第六步,将文件存到指定位置
				try
				{
					FileOutputStream fout = new FileOutputStream("E:/students.xls");
					wb.write(fout);
					fout.close();
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
		
		
		
	}
}

注意:

修改Excel中的某个内容:

cell1.setCellValue("1000");

保存修改后的Excel文件:

OutputStream out = new FileOutputStream(file);
wb.write(out);

轉自【http://blog.csdn.net/he90227/article/details/37563497】
igh ethercat 实时性测试 组备注std(us)max(us)a无锁核,无rt补丁8.38108b锁核+FIFO调度+内存锁定,无rt补丁0.9216c锁核+FIFO调度+内存锁定,+rt补丁0.9922可以看到 a组 使用ethercat不做其他优化,周期抖动非常大,且max能达到 100 多 us;通过 锁核+FIFO调度+内存锁定 进行优化,可以看到 抖动明显降低,且max 为 20 us 左右; 阅读详情

相关推荐

汽车LED驱动芯片TPS92682-Q1 SPI接口配置与调试实战指南

SPI(串行外设接口)作为一种高速、全双工的同步串行通信总线,广泛应用于微控制器与外围设备之间的数据交换。其工作原理基于主从架构,通过时钟、数据线片选信号实现设备间的精准时序控制。在汽车电子工业控制领域,SPI因其简单高效、易于扩展的特性,成为配置复杂电源管理芯片、传感器存储器的关键技术。通过SPI接口,工程师能够动态调整设备参数、实时读取状态信息,并实现复杂的故障诊断与保护策略,极大地提升了系统的灵活性与可靠性。本文以汽车级双通道LED恒流驱动器TPS92682-Q1为例,深入解析如何通过SPI接口

weixin_29185493的博客 243

POIHSSFXSSF以及SXSSF方式的区别

POI提供了HSSFXSSF以及SXSSF三种方式操作Excel。他们的区别如下:HSSF:是操作Excel97-2003版本,扩展名为.xls。XSSF:是操作Excel2007版本开始,扩展名为.xlsx。SXSSF:是在XSSF基础上,POI3.8版本开始提供的一种支持低内存占用的操作方式,扩展名为.xlsx。  其次,大家需要了解下Excel不同版本的一些区别,这些限制其实间接的局限了POI提供的API功能。1、支持的行数、列数Excel97-2003版本,一个sheet最大行数65536,最大列

wp3524的专栏 2973

cmake-3.31.10-windows-x86_64.zip

cmake-3.31.10-windows-x86_64.zip

java中关于超大Excel文件解析,XSSF,SXSSF,以及easyExcel的速度比较及其代码实现

java中关于超大Excel文件,XSSF,SXSSF,以及easyExcel的速度比较及其代码实现

weixin_49187233的博客 1880

Java利用POI:HSSFXSSF、SXSSF操作Excel

Java利用POI操作Excel POI提供了HSSFXSSF以及SXSSF三种方式操作Excel。 其中: HSSFExcel97-2003版本,扩展名为.xls。一个sheet最大行数65536,最大列数256。 XSSFExcel2007版本开始,扩展名为.xlsx。一个sheet最大行数1048576,最大列数16384。 SXSSF:是在XSSF基础上,POI3.8版本开始提供的支持低内存占用的操作方式,扩展名为.xlsx。 SXSSF支持低内存占用的方式是,通过一个滑动窗口来限制访问Row

大数据开发、JAVA开发、人工智能AI 7112

org.apache.poi.hssf.usermodel.HSSFCellStyle cannot be cast to org.apache.poi.xssf.usermodel.XSSFCell

在开发过程中,使用 Apache POI 库处理 Excel 文件时,经常会遇到 "HSSFCellStyle cannot be cast to XSSFCell" 错误。这个错误通常是因为代码中混淆了不同格式的 Excel 文件(.xls .xlsx)。本文将详细分析此错误产生的原因,并提供解决方案,帮助开发者避免类似的错误,提高工作效率。

七一 2159

java excel poi hssf_POI操作Excel详解HSSFXSSF两种方式

1 packagecom.tools.poi.lesson1;23 importjava.io.File;4 importjava.io.FileInputStream;5 importjava.io.FileNotFoundException;6 importjava.io.FileOutputStream;7 importjava.io.IOException;8 importjava.io....

weixin_42332910的博客 403

POI操作excel详解HSSFXSSF两种方式的区别

HSSF方式: [java] view plain copy print?package com.tools.poi.lesson1;    import java.io.FileInputStream;  import java.io.FileNotFoundException;  import java.io.FileOutputStream;  import java.io.IOExcepti

付石头的博客 7095

POI操作Excel详解 HSSFXSSF两种方式

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HSSF方式:pack

hftyhv的博客 653
上一篇: jQuery 常用函数
下一篇: 检测Android手机是否具有root权限和静默安装
系统信息
系统信息 领域专家: 后端开发技术领域 领域专家: 后端开发技术领域
博客等级 码龄23年 2404粉丝 565原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值