Java poi读取,写入Excel2003

Java poi读取,写入Excel2003

相关阅读:
poi读写Excel2007:http://www.cnblogs.com/gavinYang/p/3576741.html
jxl读写excel2003/2007:http://www.cnblogs.com/gavinYang/p/3576819.html

package com.gavin.operational.excle;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;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.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;public class ReadExcelXls {public static void main(String[] args) throws Exception{//读取xlsMap<Integer, List<String[]>> map = readXls("E:/测试读取XLS.xls");for(int n=0;n<map.size();n++){List<String[]> list = map.get(n);System.out.println("-------------------------sheet"+n+"--------------------------------");for(int i=0;i<list.size();i++){String[] arr = (String[]) list.get(i);for(int j=0;j<arr.length;j++){if(j==arr.length-1)System.out.print(arr[j]);elseSystem.out.print(arr[j]+"|");}System.out.println();}}//写入xlswriteXls("E:/测试写入XLS.xls",map);}//读取xlspublic static Map<Integer, List<String[]>> readXls(String fileName) {Map<Integer, List<String[]>> map = new HashMap<Integer, List<String[]>>();try {InputStream is = new FileInputStream(fileName);HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);// 循环工作表Sheet  for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);if (hssfSheet == null) {continue;}List<String[]> list = new ArrayList<String[]>();//总行数:hssfSheet.getLastRowNum()-hssfSheet.getFirstRowNum()+1);// 循环行Rowfor (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {HSSFRow hssfRow = hssfSheet.getRow(rowNum);if (hssfRow == null) {continue;}String[] singleRow = new String[hssfRow.getLastCellNum()];for(int column=0;column<hssfRow.getLastCellNum();column++){Cell cell = hssfRow.getCell(column,Row.CREATE_NULL_AS_BLANK);switch(cell.getCellType()){case Cell.CELL_TYPE_BLANK:singleRow[column] = "";break;case Cell.CELL_TYPE_BOOLEAN:singleRow[column] = Boolean.toString(cell.getBooleanCellValue());break;case Cell.CELL_TYPE_ERROR:singleRow[column] = "";break;case Cell.CELL_TYPE_FORMULA:cell.setCellType(Cell.CELL_TYPE_STRING);singleRow[column] = cell.getStringCellValue();if (singleRow[column] != null) {singleRow[column] = singleRow[column].replaceAll("#N/A", "").trim();}break;case Cell.CELL_TYPE_NUMERIC:if (DateUtil.isCellDateFormatted(cell)) {singleRow[column] = String.valueOf(cell.getDateCellValue());} else {cell.setCellType(Cell.CELL_TYPE_STRING);String temp = cell.getStringCellValue();// 判断是否包含小数点,如果不含小数点,则以字符串读取,如果含小数点,则转换为Double类型的字符串if (temp.indexOf(".") > -1) {singleRow[column] = String.valueOf(new Double(temp)).trim();} else {singleRow[column] = temp.trim();}}break;case Cell.CELL_TYPE_STRING:singleRow[column] = cell.getStringCellValue().trim();break;default:singleRow[column] = "";break;}}list.add(singleRow);}map.put(numSheet, list);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return map;}  //写入xlspublic static void writeXls(String fileName,Map<Integer,List<String[]>> map) {try {HSSFWorkbook wb = new HSSFWorkbook();for(int sheetnum=0;sheetnum<map.size();sheetnum++){HSSFSheet sheet = wb.createSheet(""+sheetnum);List<String[]> list = map.get(sheetnum);for(int i=0;i<list.size();i++){HSSFRow row = sheet.createRow(i);String[] str = list.get(i);for(int j=0;j<str.length;j++){HSSFCell cell = row.createCell(j);cell.setCellValue(str[j]);    }}}FileOutputStream outputStream = new FileOutputStream(fileName);wb.write(outputStream);outputStream.close();} catch (FileNotFoundException e) {// TODO 自动生成的 catch 块
            e.printStackTrace();} catch (IOException e) {// TODO 自动生成的 catch 块
            e.printStackTrace();}}}

 

转载于:https://www.cnblogs.com/gavinYang/p/3576739.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421161.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

leetcode 21 合并两个有序链表 C++

因为迭代比较好理解 所以我们在这里用递归 class Solution { public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {if(!l1) return l2;else if(!l2) return l1;else if(l1->val<l2->val){l1->nextmergeTwoLists(l1->next,l2);return l1;}else{l2->…

1061. 判断题(15)

1061. 判断题(15) 时间限制400 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue判断题的评判很简单&#xff0c;本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分。 输入格式&#xff1a; 输入在第一行给出两个不超过100的正整数N和M&…

css和 js 改变html里面的定位。

css和 js 改变html里面的定位。&#xff08;三种方式&#xff09; <style type"text/css">#div1{border: 1px aquamarine solid;/*固定定位&#xff1a;此元素在整个网页的位置不变,固定某处不动*/position : fixed;left: 20px;top: 10px;}#div2{/*相对定位&am…

终端I/O之行控制函数

下列4个函数提供了终端设备的行控制能力。其中&#xff0c;filedes引用一个终端设备&#xff0c;否则出错返回&#xff0c;errno设置为ENOTTY。 #include <termios.h> int tcdrain(int filedes); int tcflow(int filedes, int action); int tcflush(int filedes, int que…

js 正则表达式实现文本验证

<style>.spa{/*js来改变span颜色*/color: red;}</style><script type"text/javascript">function checkIn(obj){//失去焦点&#xff08;鼠标离开文本框&#xff09;时验证//文本框的值var val obj.value;//拿到文本框的名字&#xff0c;用来做swi…

1060. 爱丁顿数(25)

1060. 爱丁顿数(25) 时间限制250 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue英国天文学家爱丁顿很喜欢骑车。据说他为了炫耀自己的骑车功力&#xff0c;还定义了一个“爱丁顿数”E&#xff0c;即满足有E天骑车超过E英里的最大整数E。据说爱丁顿自己的…

leetcode 203 移除链表元素 C++

注意第一个元素的处理方法 今晚还有四个题要做&#xff0c;来不及解释了&#xff0c;上代码 class Solution { public:ListNode* removeElements(ListNode* head, int val) {struct ListNode*prenew ListNode(0,head);struct ListNode*temppre;if(!temp->next) return null…

unity3d由多个部分组成一个角色

摘自http://forum.unity3d.com/threads/16485-quot-stitch-multiple-body-parts-into-one-character-quot So I have many many models. Each has a skeleton, material, etc. I want to take some of these and combine them into one so I can apply animation commands to …

求质数的个数

/我买了一本书&#xff0c;意大利作家 保罗.乔尔达诺 得奖的一本小说&#xff0c;好奇这本书有多少页。但它每页页码都是质数&#xff0c;需求如下&#xff0c;1907页书&#xff0c;每个页数以质数命名&#xff0c;求有多少页。/ package funJoy;public class calBook {public…

leetcode 701 二叉搜索树的插入操作 C++ 递归和迭代

迭代 class Solution { public:TreeNode* insertIntoBST(TreeNode* root, int val) {if(!root) return new TreeNode(val);TreeNode*curroot;while(cur){if(val<cur->val){if(cur->left)curcur->left;else{cur->leftnew TreeNode(val);break;}}else if(val>…

1062. 最简分数(20)

1062. 最简分数(20) 时间限制400 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue一个分数一般写成两个整数相除的形式&#xff1a;N/M&#xff0c;其中M不为0。最简分数是指分子和分母没有公约数的分数表示形式。 现给定两个不相等的正分数 N1/M1 和 N2/M…

解决ListView 缓存机制带来的显示不正常问题

ListView加载数据原理:系统绘制ListView时&#xff0c;首先会用getCount&#xff08;&#xff09;函数得到要绘制的这个列表的长度&#xff0c;然后开始逐行绘制。然后调用getView()函数&#xff0c;在这个函数里面首先获得一个View&#xff08;简单item&#xff0c;如字符串或…

Java里面static, final, this, super, 代码块, 单例模式

static关键字 package csdn;public class staticWord {public static void main(String[] args) {funStatic fs1 new funStatic();funStatic.showA();fs1.b 2;fs1.showB(); // 2System.out.println("----------");funStatic fs2 new funStatic();funStatic.a 3…

1063. 计算谱半径(20)

1063. 计算谱半径(20) 时间限制200 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue在数学中&#xff0c;矩阵的“谱半径”是指其特征值的模集合的上确界。换言之&#xff0c;对于给定的n个复数空间的特征值{a1b1i, ..., anbni}&#xff0c;它们的模为实部…

用了vscode和clion我都裂开了

vscode看着挺牛逼&#xff0c;经常遇到问题&#xff0c;每次运行还得f5调试&#xff0c;你如果想便捷一点&#xff0c;你可以去下插件&#xff0c;比如ccmopiler&#xff0c;code runner啥的。我自己用c但是这两个玩意都不支持c11想要支持还得自己配你看4S店上很多文章&#xf…

windows phone (12) 小试自定义样式

windows phone (12) 小试自定义样式 原文:windows phone (12) 小试自定义样式样式在BS开发中经常用到&#xff0c;在wp中系统也提供了解决办法&#xff0c;就是对设置的样式的一种资源共享&#xff0c;首先是共享资源的位置&#xff0c;它是在App类中&#xff0c;之前我们已经有…

xdoj判断堆栈出栈序列是否有效c++

我在leetcode上写过类似的题&#xff0c;这个代码在xdoj上只能得***50***分&#xff0c;跪求各位大佬挑挑毛病。 #include<stack> #include<iostream> #include<vector> using namespace std; int main(){vector<int>poped;stack<int>s;int n,t;…

1064. 朋友数(20)

1064. 朋友数(20) 时间限制400 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者CHEN, Yue如果两个整数各位数字的和是一样的&#xff0c;则被称为是“朋友数”&#xff0c;而那个公共的和就是它们的“朋友证号”。例如123和51就是朋友数&#xff0c;因为123 51 6&…

Java StringBuffer

最近在学Java API 做的简单笔记 package C12_17;public class testBuffer {public static void main(String[] args) {//StringBuffer 属于java.lang包&#xff0c;属于基础包&#xff0c;jdk api 里面有其运用方式。StringBuffer sb new StringBuffer("Hello");//…

XDOJ 363 输出快速排序递归算法隐含递归树的后序遍历序列 AC

像我这样的菜鸡也没有什么能输出的&#xff0c;好像我写题解也不算输出。 最近期末了&#xff0c;写数据结构实验的时候&#xff0c;这个题写了挺久的&#xff0c;搞出来记录一下。 输出快速排序递归算法隐含递归树的后序遍历序列 描述&#xff1a; 快速排序递归算法隐含一棵由…