




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.text.DecimalFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Arrays;import java.util.Date;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFDateUtil;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.poifs.filesystem.POIFSFileSystem;public class EditExcel public static void main(String args) String path = f:/#usertemple.xls;/ 文件路径try File files = new File(path);List list=getDataList(files); /读取if(list!=null&list.size()0)for(String str:list)for(int i=0;istr.length;i+)System.out.print(stri+t);System.out.println();outExcel(list); /poi导出Excel catch (Exception e) e.printStackTrace();/* * 读取EXCEL * * param firstrow * 从第几行开始读取 * return 读取后返回数组 */public static String getData(File file, int firstrow) throws FileNotFoundException, IOException List result = new ArrayList();int rowSize = 0;BufferedInputStream in = new BufferedInputStream(new FileInputStream(file);POIFSFileSystem fs = new POIFSFileSystem(in);HSSFWorkbook wb = new HSSFWorkbook(fs);HSSFCell cell = null;for (int sheetIndex = 0; sheetIndex wb.getNumberOfSheets(); sheetIndex+) HSSFSheet st = wb.getSheetAt(sheetIndex);for (int rowIndex = firstrow; rowIndex rowSize) rowSize = tempRowSize;String values = new StringrowSize;Arrays.fill(values, );boolean hasValue = false;for (short columnIndex = 0; columnIndex = row.getLastCellNum(); columnIndex+) String value = ;cell = row.getCell(columnIndex);if (cell != null) switch (cell.getCellType() case HSSFCell.CELL_TYPE_STRING:/ 读取的格式为字符串value = cell.getStringCellValue();break;case HSSFCell.CELL_TYPE_NUMERIC:/ 读取的格式为数组if (HSSFDateUtil.isCellDateFormatted(cell) Date date = cell.getDateCellValue();if (date != null) value = new SimpleDateFormat(yyyy-MM-dd).format(date); else value = ; else value = new DecimalFormat(0.00).format(cell.getNumericCellValue();break;case HSSFCell.CELL_TYPE_FORMULA:value = ;break;/ 导入时如果为空case HSSFCell.CELL_TYPE_BLANK:break;case HSSFCell.CELL_TYPE_ERROR:value = ;break;/ 导入时如果为BOOLEAN型 自定义格式输出case HSSFCell.CELL_TYPE_BOOLEAN:value = (cell.getBooleanCellValue() = true ? Y: N);break;default:value = ;valuescolumnIndex = rightTrim(value);hasValue = true;if (hasValue) result.add(values);in.close();String returnArray = new Stringresult.size()rowSize;for (int i = 0; i returnArray.length; i+) returnArrayi = (String) result.get(i);return returnArray;return null;public static List getDataList(File file) throws FileNotFoundException, IOException List result = new ArrayList();int rowSize = 0;BufferedInputStream in = new BufferedInputStream(new FileInputStream(file);POIFSFileSystem fs = new POIFSFileSystem(in);HSSFWorkbook wb = new HSSFWorkbook(fs);HSSFCell cell = null;for (int sheetIndex = 0; sheetIndex wb.getNumberOfSheets(); sheetIndex+) HSSFSheet st = wb.getSheetAt(sheetIndex);for (int rowIndex = 0; rowIndex rowSize) rowSize = tempRowSize;String values = new StringrowSize;Arrays.fill(values, );boolean hasValue = false;for (short columnIndex = 0; columnIndex = 0; i-) if (str.charAt(i) != 0x20) break;length-;return str.substring(0, length);/* * 作者: * 时间: * 说明:poi导出Excel * 使用地方: * param list * throws IOException */public static void outExcel(List list) throws IOException/ 第一步,创建一个webbook,对应一个Excel文件HSSFWorkbook wb = new HSSFWorkbook();/ 第二步,在webbook中添加一个sheet,对应Excel文件中的sheetHSSFSheet sheet = wb.createSheet(表一);/ 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制shortHSSFRow 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);/ 第五步,写入实体数据 实际应用中这些数据从数据库得到,int i=0;for (String str:list)row = sheet.createRow(int) i + 1);System.out.println(str=+i);for(int j=0;jstr.length;j+)/ 第四步,创建单元格,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 行测国考答案及题库及完整答案详解1套
- 西藏农牧学院期末考试卷及答案
- 国考题库综合及参考答案详解(能力提升)
- 生成式人工智能技术的伦理治理研究
- 2025第三人民医院透析急性反应处理考核
- 基于代价函数消除与多矢量优化的共母线型开绕组永磁同步电机模型预测转矩控制策略研究
- 鄂尔多斯市人民医院器官功能衰竭预警考核
- 2025中医院增强现实导航技术考核
- 邯郸市中医院肌电图室管理考核
- 冻融循环作用下泡沫混凝土动态力学特性及损伤机理研究
- 心灵树洞中小学生心理健康主题班会PPT教学课件
- 低血糖症-课件
- 木质纤维素的生物分解及其转化技术
- 海康威视磁盘阵列使用说明精.选
- GB/T 7387-1999船用参比电极技术条件
- GB/T 39473-2020北斗卫星导航系统公开服务性能规范
- GB 16808-2008可燃气体报警控制器
- 公司有限空间作业安全专项排查表
- 强度调制机理光纤传感器基本原理课件
- 《当代中国经济》第一章中国经济体制改革
- 《自强不息的人格修养》-课件1
评论
0/150
提交评论