根据excel模板里设置指定的num0,num1,num2,num3.然后替换成想到的内容,生成新的excel文件
import org.apache.poi.hssf.usermodel.HSSFCell; 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; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * Writer: fuanyu * Date : 14-12-26 * Time : 上午9:43 */ @SuppressWarnings("unchecked") public class TestExcel { /** * 替换Excel模板文件内容 * @param item 文档数据 * @param sourceFilePath Excel模板文件路径 * @param targetFilePath Excel生成文件路径 */ public static boolean replaceModel(Map item, String sourceFilePath, String targetFilePath) { boolean bool = true; try { POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream(sourceFilePath)); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); Iterator rows = sheet.rowIterator(); while(rows.hasNext()){ HSSFRow row = (HSSFRow) rows.next(); if(row!=null) { int num = row.getLastCellNum(); for(int i=0;i<num;i++) { HSSFCell cell= row.getCell(i); if(cell!=null) { cell.setCellType(HSSFCell.CELL_TYPE_STRING); } if(cell==null || cell.getStringCellValue()==null) { continue; } String value= cell.getStringCellValue(); if(!"".equals(value)) { Set<String> keySet = item.keySet(); Iterator<String> it = keySet.iterator(); while (it.hasNext()) { String text = it.next(); if(value.equalsIgnoreCase(text)) { cell.setCellValue((String)item.get(text)); break; } } }else{ cell.setCellValue(""); } } } } // 输出文件 FileOutputStream fileOut = new FileOutputStream(targetFilePath); wb.write(fileOut); fileOut.close(); } catch (Exception e) { bool = false; e.printStackTrace(); } return bool; } public static void main(String[] args) { Map item = new HashMap(); item.put("num0","000"); item.put("num1","1"); item.put("num2","2"); item.put("num3","3"); item.put("num4","4"); item.put("num5","5"); item.put("num6","6"); //d:\\template.xls为Excel模板文件,d:\\test.xls为程序根据Excel模板文件生成的新文件 replaceModel(item, "d:\\template.xls", "d:\\test.xls"); } }
相关推荐
java 读excel 写 excel 边读边写excel 可以数据库抓来数据List,填冲到excel的制定位置
通过java操作excel表格的工具类库 支持Excel 95-2000的所有版本 生成Excel 2000标准格式 支持字体、数字、日期操作 能够修饰单元格属性 支持图像和图表 应该说以上功能已经能够大致满足我们的需要...
两个String字符串修改完了之后,直接运行使用 ExcelOCR(表格识别)中右键直接run, ①本地读取图片的路径默认为 D:pic文件夹,请自己建一个,然后表格图片放进去 ②默认表格图片识别后,excle文件输出到桌面test...
实例42 字符串索引越界异常(StringIndexOutBounds) 60 实例43 操作错误(UnsupportedOperationException) 60 4.2 运行时异常 61 实例44 找不到指定类时发生的异常(ClassNotFoundException) 62 ...
实例42 字符串索引越界异常(StringIndexOutBounds) 60 实例43 操作错误(UnsupportedOperationException) 60 4.2 运行时异常 61 实例44 找不到指定类时发生的异常(ClassNotFoundException) 62 ...
实例42 字符串索引越界异常(StringIndexOutBounds) 60 实例43 操作错误(UnsupportedOperationException) 60 4.2 运行时异常 61 实例44 找不到指定类时发生的异常(ClassNotFoundException) 62 实例45 请求的...
核心功能为java导出pdf,只需要准备一段html字符串就能导出,很简单的操作. 其它功能: 1,纯js无刷新文件上传下载; 2,纯js无刷新分页及增删改查; 3,仿百度搜索智能提示,自动补全; 4,多功能组合框的应用,既...
实例40 替换字符串中的部分字符 92 实例41 Java字符串之密码加密 93 实例42 正则表达式验证字符串 95 第7章 Java常用类 98 实例43 数字的舍入 98 实例44 转换数字的进制 101 实例45 随机数 103 实例46 Java ...
实例42 字符串索引越界异常(StringIndexOutBounds) 60 实例43 操作错误(UnsupportedOperationException) 60 4.2 运行时异常 61 实例44 找不到指定类时发生的异常(ClassNotFoundException) 62 实例45 请求的...
实例42 字符串索引越界异常(StringIndexOutBounds) 60 实例43 操作错误(UnsupportedOperationException) 60 4.2 运行时异常 61 实例44 找不到指定类时发生的异常(ClassNotFoundException) 62 实例45 请求...
实例076 批量替换某一类字符串 95 实例077 把异常与错误信息显示到窗体中 97 实例078 从字符串中分离文件路径、 文件名及扩展名 98 实例079 判断手机号的合法性 99 实例080 用字符串构建器追加字符 100 实例081 去掉...
比如,我们可以使用Document.Replace方法替换文档中的指定文本,使用Document.InsertText方法插入新的文本内容,使用Document.FindString方法查找文档中的指定字符串等。 此外,spire.doc.free还支持设置文档的样式...
《MATLAB实用教程》从快速入门和实用性两个方面,对MATLAB7.0进行了详细的介绍,并列举了大量实用的例子。全书共分为12章,第1章介绍MATLAB的...2.4.3 字符串的查找和替换 2.4.4 字符串与数值间的转换 习题 . . . .
普通字符串替换为 ${string} 表格循环用标签 姓名:${user.userName} , 性别:${user.sex} 2、将word模板另存为xml格式 3、将xml模板文件后缀名改为.ftl 4、编辑ftl文件 注意 编辑word模板时,${string} ...
6.4.4 Unicode字符串操作符(uU) 6.5 内建函数 6.5.1 标准类型函数 6.5.2 序列类型函数 6.5.3 字符串类型函数 6.6 字符串内建函数 6.7 字符串的独特特性 6.7.1 特殊字符串和...
6.4.4 Unicode字符串操作符(u/U) 6.5 内建函数 6.5.1 标准类型函数 6.5.2 序列类型函数 6.5.3 字符串类型函数 6.6 字符串内建函数 6.7 字符串的独特特性 6.7.1 特殊字符串...
matlab代码替换xlwrite Matlab的xlswrite函数的独立于平台的替代方案 问题 Matlab的xlswrite仅在安装了Excel的Windows平台上写入xls和xlsx文件。 这是因为它使用的COM对象仅在Windows上受支持。 解决方案 xlwrite是...