关于excle导数据的一些代码笔记
package com.bonc.util; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream; public class CopyExcelTest { /**
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public static void copyFile(String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File("D:/aa/modul2.xls");
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream("D:/aa/modul2.xls"); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
} public static void main(String[] args) {
copyFile("D:/aa/new2.xls");
} }
package com.bonc.util; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.bonc.bean.Huzhu;
import com.bonc.bean.Person; /**
* 操作Excel表格的功能类
*/
public class ExcelReader { public static List<Huzhu> showExcel(String path) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File(path)));
HSSFSheet sheet = null; List<Huzhu> huzhuList = new ArrayList<>();
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 获取每个Sheet表
sheet = workbook.getSheetAt(i);
for (int j = 0; j < sheet.getLastRowNum() + 1; j++) {// getLastRowNum,获取最后一行的行标
HSSFRow row = sheet.getRow(j);
if (row != null) {
if (row.getCell(1).toString().equals("a户主")){
Huzhu huzhu = new Huzhu(row.getCell(0).toString(), row.getCell(1).toString(),
row.getCell(2).toString(), row.getCell(3).toString(),
row.getCell(4).toString()+row.getCell(5).toString(),j);
huzhuList.add(huzhu);
}
}
//System.out.println(""); // 读完一行后换行
}
//System.out.println(sheet.getLastRowNum());
//System.out.println("读取sheet表:" + workbook.getSheetName(i) + " 完成");
} return huzhuList;
} public static List<Huzhu> getPerson(List<Huzhu> huzhuLists,String path) throws FileNotFoundException, IOException{ HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File(path)));
HSSFSheet sheet = null;
sheet = workbook.getSheetAt(0); int size = huzhuLists.size();
int []nums = new int[size+1];
for (int i = 0; i < nums.length - 1; i++) {
Huzhu huzhu = huzhuLists.get(i);
nums[i] = huzhu.getIndex();
}
nums[size] = sheet.getLastRowNum();
int[] sumTotle = sumTotle(nums);
// System.out.println("共户:" + size);
// System.out.println("计算出:" + sumTotle.length);
List<Huzhu> huzhuListAndPerson = new ArrayList<>();
for (int i = 0; i < size; i++) {
List<Person> personList = new ArrayList<>();
Huzhu huzhu = huzhuLists.get(i);
int huzhuIndex = huzhu.getIndex();
int personNum = sumTotle[i];
for (int j = 1; j <= personNum; j++) {
HSSFRow row = sheet.getRow(huzhuIndex + j);
Person person = new Person(row.getCell(1).toString(), row.getCell(2).toString(),
row.getCell(3).toString(), row.getCell(4).toString()+row.getCell(5).toString());
personList.add(person);
}
huzhu.setPerson(personList);
huzhuListAndPerson.add(huzhu);
}
return huzhuListAndPerson;
} private static int[] sumTotle(int[] s){ int []temp = new int[s.length-1];
for (int i = 0; i < s.length - 1; i++) {
temp[i] = s[i+1] - s[i] - 1;
}
return temp; } public static void main(String[] args) throws IOException { int []temp = new int[3];
temp[0] = 2;
temp[1] = 48;
temp[2] = 1851;
int[] is = sumTotle(temp);
System.out.println(Arrays.toString(is)); }
}
package com.bonc.util; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class SetValues { private String path; private HSSFSheet sheet; public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
} private static HSSFWorkbook wb = null; private static FileOutputStream os = null; public SetValues(String path) throws FileNotFoundException, IOException{
this.wb = new HSSFWorkbook(new FileInputStream(path));
this.os = new FileOutputStream(path);
this.sheet = this.wb.getSheetAt(0);
} public static void main(String[] args) throws FileNotFoundException, IOException {
String filePath = "D:\\aa\\new2.xls";
SetValues setValues = new SetValues(filePath);
setValues.setValue( 15, 4, new HSSFRichTextString("汉族1"));
//setValues.setValue( 16, 2, new HSSFRichTextString("汉族"));
//setValues.setValue(filePath, 3, 12, new HSSFRichTextString("高亚伟"));
//new SetValues().setValue(filePath, 14, 5, new HSSFRichTextString("高亚2伟"));
setValues.setValue(4, 1, new HSSFRichTextString("高亚1伟"));
setValues.flush();
setValues.shutDown();
} public HSSFWorkbook getWb() {
return wb;
}
public void setWb(HSSFWorkbook wb) {
SetValues.wb = wb;
} public FileOutputStream getOs() {
return os;
} public void setOs(FileOutputStream os) {
SetValues.os = os;
} public void setValue(int x, int y, HSSFRichTextString val) {
try {
HSSFRow row = sheet.getRow(x);
HSSFCell cell = row.getCell((short) y);
cell.setCellValue(val);
wb.setForceFormulaRecalculation(true);
} catch (Exception e) {
e.printStackTrace();
} }
public void flush(){
try {
wb.write(os);
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
} public void shutDown() throws IOException{
if (os != null) {
os.close();
}
} }
关于excle导数据的一些代码笔记的更多相关文章
- 【hadoop代码笔记】Mapreduce shuffle过程之Map输出过程
一.概要描述 shuffle是MapReduce的一个核心过程,因此没有在前面的MapReduce作业提交的过程中描述,而是单独拿出来比较详细的描述. 根据官方的流程图示如下: 本篇文章中只是想尝试从 ...
- 【hadoop代码笔记】hadoop作业提交之汇总
一.概述 在本篇博文中,试图通过代码了解hadoop job执行的整个流程.即用户提交的mapreduce的jar文件.输入提交到hadoop的集群,并在集群中运行.重点在代码的角度描述整个流程,有些 ...
- 【Hadoop代码笔记】目录
整理09年时候做的Hadoop的代码笔记. 开始. [Hadoop代码笔记]Hadoop作业提交之客户端作业提交 [Hadoop代码笔记]通过JobClient对Jobtracker的调用看详细了解H ...
- <Python Text Processing with NLTK 2.0 Cookbook>代码笔记
如下是<Python Text Processing with NLTK 2.0 Cookbook>一书部分章节的代码笔记. Tokenizing text into sentences ...
- [学习笔记] SSD代码笔记 + EifficientNet backbone 练习
SSD代码笔记 + EifficientNet backbone 练习 ssd代码完全ok了,然后用最近性能和速度都非常牛的Eifficient Net做backbone设计了自己的TinySSD网络 ...
- DW网页代码笔记
DW网页代码笔记 1.样式. class 插入类样式 标签技术(html)解决页面的内容样式技术(css)解决页面的外观脚本技术 解决页面动态交互问题<form> ...
- 前端学习:JS(面向对象)代码笔记
前端学习:JS(面向对象)代码笔记 前端学习:JS面向对象知识学习(图解) 创建类和对象 创建对象方式1调用Object函数 <body> </body> <script ...
- 资源 | 数十种TensorFlow实现案例汇集:代码+笔记
选自 Github 机器之心编译 参与:吴攀.李亚洲 这是使用 TensorFlow 实现流行的机器学习算法的教程汇集.本汇集的目标是让读者可以轻松通过案例深入 TensorFlow. 这些案例适合那 ...
- 【Hadoop代码笔记】Hadoop作业提交之TaskTracker获取Task
一.概要描述 在上上一篇博文和上一篇博文中分别描述了jobTracker和其服务(功能)模块初始化完成后,接收JobClient提交的作业,并进行初始化.本文着重描述,JobTracker如何选择作业 ...
随机推荐
- C++ Primer学习笔记_1_快速入门
C++快速入门 一 编写简单程序 // main是操作系统唯一显示调用的函数int main() {/**return返回的值是一个状态指示器 0:成功 非0:返回错误给OS*以echo $?命令可以 ...
- ubuntu包管理机制
1 ubuntu包管理机制 跟大家分享一下ubuntu的软件管理机制.如果你们有过: apt-get install 或者 apt-get update 失败的经历. 在众多的apt命令中迷失. 疑惑 ...
- 学习笔记 - Manacher算法
Manacher算法 - 学习笔记 是从最近Codeforces的一场比赛了解到这个算法的~ 非常新奇,毕竟是第一次听说 \(O(n)\) 的回文串算法 我在 vjudge 上开了一个[练习],有兴趣 ...
- MySQL 开启事件 使用定时器调用存储过程
mysql定时器是系统给提供了event,而oracle里面的定时器是系统给提供的job.废话少说,下面创建表:create table mytable (id int auto_incremen ...
- 利用SoapUI 测试web service的一些问题总结
总结两个利用SoapUI 测试web service的一些问题: 1.请求一个soap service 请求的时候:按照下面的配置输入请求地址后, 2.根据实际service接口的需要,传入相应的参数 ...
- 在tornado中使用异步mysql操作
在使用tornado框架进行开发的过程中,发现tornado的mysql数据库操作并不是一步的,造成了所有用户行为的堵塞.tornado本身是一个异步的框架,要求所有的操作都应该是异步的,但是数据库这 ...
- MVC Controller 基类 BaseController 中的 Request
今天修复mvc中的一个bug,需求是每个页面要获取当前URL链接中 host首是否正确,我把获取url的方法写到了Controller的基类BaseController(BaseController继 ...
- 使用PHPExcel 对表格进行,读取和写入的操作。。。。
下面的代码是使用PHPExcel 对多个表格数据进行读取, 然后整合的写入新的表格的方法!!!代码有点粗糙 , 多多保函!!! 这里有些地方注意下,如果你的表格数据过大, 一定要记得修改php.ini ...
- 【Hive六】Hive调优小结
Hive调优 Hive调优 Fetch抓取 本地模式 表的优化 小表.大表Join 大表Join大表 MapJoin Group By Count(Distinct) 去重统计 行列过滤 动态分区调整 ...
- SSH Secure :Algorithm negotiation failed,反复提示输入password对话框
在嵌入式开发中,SSH Secure File Transfer Client 软件使用,方便了windows和linux之间文件拷贝,尤其是多台主机状况下. 最近装了Ubuntu 16.0.4,在V ...