




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 矿山建设工程合同范本
- 装饰木板采购合同范本
- 劳务合同范本文库
- 获得性大疱性表皮松解症护理查房
- 供暖服务评估合同
- 混凝土单包工合同范本
- 自建铺面出售合同范本
- 简易加盟餐饮合同范本
- 订单农业合同范本 俄语
- 景区托管协议合同范本
- 粮食仓储(粮库)安全生产标准化管理体系全套资料汇编(2019-2020新标准实施模板)
- 喜茶运营管理手册和员工操作管理手册
- 比亚迪汉DM-i说明书
- 心肾综合征及其临床处理
- 普通高中课程方案
- 2022年山东高考生物试卷真题及答案详解(精校版)
- GB/T 38936-2020高温渗碳轴承钢
- 高考地理一轮复习课件 【知识精讲+高效课堂】 农业区位因素及其变化
- 教师专业发展与名师成长(学校师范专业公共课)
- 互通立交设计课件
- 生物竞赛辅导 动物行为学第七章 行为发育(38)课件
评论
0/150
提交评论