java poi 提取txt文件内容添加到excel

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java poi 提取txt文件内容添加到excel相关的知识,希望对你有一定的参考价值。

我提取txt文档中的数据,怎么在提取所有数据时添加条件提取个别的关键字并且输出到另一个excel文件中(这个文件是已存在的并且是以插入的方式输入的)我的代码怎么不成立呢? 请各位大神帮帮忙吧 !
我的代码如下:

public void readTxtYsx()//读文件
try
String encoding="GBK";//// 字符编码(可解决中文乱码问题 )
File file=new File("D:\\CRM-BOSS-BASS-EC.txt.txt");//句柄
if(file.isFile() && file.exists()) //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt=null;
while((lineTxt = bufferedReader.readLine()) != null)

WriteYSX(row++,lineTxt.toString().trim());

ofs = new FileOutputStream(filePath);
wb.write(ofs);
ofs.flush();
read.close();

else
System.out.println("找不到指定的文件");

catch (Exception e)
System.out.println("读取文件内容出错");
e.printStackTrace();


public void WriteYSX( int a ,String str) throws IOException //写如EXCEL

String arr[] = str.split("#%#");
sheet = wb.getSheetAt(0);
HSSFRow hssfrow = null ;
HSSFCell celldate=null;
hssfrow = sheet.createRow(a);
for (int i = 0; i < arr.length; i++)

celldate = hssfrow.createCell(i);
celldate.setCellValue(arr[i]);

HSSFRow row=sheet.getRow(a);
celldate=row.getCell(2);
//System.out.println(celldate.getStringCellValue());
if(celldate.getStringCellValue().equals("市场经营部"))

sheet = wb.getSheetAt(1);
row=sheet.getRow(a);
// //遍历第一行所有的列(单元格)
for(int j=0;j<arr.length;j++)

Cell cell = row.getCell(j); //获取第一个单元格
//在这我怎么把我想要取的一行中有“市场经营字段的”这个行添加到我的另一个已存在的excel中呢。?
System.out.println(cell.getRichStringCellValue().getString());


System.out.println("iii----"+celldate.getStringCellValue()+"++++++++++++++"+"行"+a);

参考技术A 帮你在网上找了以下几个文章,可以解决你的问题,请参考。

Java POI Excel( pio:纯java操作excel的api )
http://www.cnblogs.com/zqmingok/archive/2010/04/16/1713480.html

java的poi技术读取和导入Excel

http://www.cnblogs.com/hongten/archive/2012/02/22/java2poi.html

java poi 如何获取获得excel的总的行数和列数?
http://zhidao.baidu.com/question/220424660.html

利用Java+POI 读写Excel文档&向Excel中插入图片
http://www.blogjava.net/renyangok/articles/63889.html

-------------------------------------------------------------------------------------------------

如果满意,请采纳。
参考技术B 你这里有什么具体的问题?

row=sheet.getRow(a);

这一部,我觉得可能会有问题,如果你这个行不存在的话,可能得到的是一个NULL对象,
因为现在你在是插入数据。所以要创建行。
参考技术C 这是部分代码。

Excel中的行,不应该是和TXT文件的行完全对应的。
EXCEL 空的时候,可能是相应的;非空时,就不同了。
参考技术D 建议你用PageOffice,能帮你解决这个问题。很专业的。

java 数据导入到exc ,并下载

package com.lizi.admin.controller.platform.excel;

import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelUtil {
//创建HSSFWorkbook工作薄对象
public static HSSFWorkbook export_text(List<Map<String,Object>> list,String data){
try {
//创建工作薄对象
HSSFWorkbook wb = new HSSFWorkbook();
//创建标题行样式
HSSFCellStyle headStyle = headStyle(wb);
//创建内容行样式
HSSFCellStyle contentStyle = contentStyle(wb);

//创建表
HSSFSheet sheet_1 = wb.createSheet(data+"对账信息");
//设置表的默认列宽
sheet_1.setDefaultColumnWidth(30);

//创建标题行
HSSFRow headRow = sheet_1.createRow(0);
HSSFCell head_cell_1 = headRow.createCell(0); //创建标题行第一列
head_cell_1.setCellValue("对账时间"); //第一列内容
head_cell_1.setCellStyle(headStyle); //将标题行样式添加

HSSFCell head_cell_2 = headRow.createCell(1);
head_cell_2.setCellValue("商家总流水");
head_cell_2.setCellStyle(headStyle);

HSSFCell head_cell_3 = headRow.createCell(2);
head_cell_3.setCellValue("商家总流水笔数");
head_cell_3.setCellStyle(headStyle);

HSSFCell head_cell_4 = headRow.createCell(3);
head_cell_4.setCellValue("实际收单总流水");
head_cell_4.setCellStyle(headStyle);

HSSFCell head_cell_5 = headRow.createCell(4);
head_cell_5.setCellValue("实际收单总流水笔数");
head_cell_5.setCellStyle(headStyle);

HSSFCell head_cell_6 = headRow.createCell(5);
head_cell_6.setCellValue("差异金额");
head_cell_6.setCellStyle(headStyle);

HSSFCell head_cell_7 = headRow.createCell(6);
head_cell_7.setCellValue("差异笔数");
head_cell_7.setCellStyle(headStyle);

HSSFCell head_cell_8 = headRow.createCell(7);
head_cell_8.setCellValue("调账金额");
head_cell_8.setCellStyle(headStyle);

HSSFCell head_cell_9 = headRow.createCell(8);
head_cell_9.setCellValue("调账笔数");
head_cell_9.setCellStyle(headStyle);

HSSFCell head_cell_10 = headRow.createCell(9);
head_cell_10.setCellValue("对账单状态");
head_cell_10.setCellStyle(headStyle);

//为内容行添加数据和样式
for (int i = 1; i <= list.size(); i++) {
HSSFRow contentRow = sheet_1.createRow(i);
Map<String, Object> map=list.get(i-1);
String accountsTime=map.get("accountsTime").toString();
String sysMoney=map.get("sysMoney").toString();
String sysNum=map.get("sysNum").toString();
String passagewayMoney=map.get("passagewayMoney").toString();
String passagewayNum=map.get("passagewayNum").toString();
String differenceMoney=map.get("differenceMoney").toString();
String differenceNumber=map.get("differenceNumber").toString();
String adjustmentMoney=map.get("adjustmentMoney").toString();
String adjustmentNumber=map.get("adjustmentNumber").toString();
String state="";
if(map.get("state")!=null){
if(map.get("state").toString().equals("0")){
state="正常";
}
if(map.get("state").toString().equals("1")){
state="未对账";
}
if(map.get("state").toString().equals("2")){
state="已对账";
}
}
HSSFCell content_cell_1 = contentRow.createCell(0);
content_cell_1.setCellValue(accountsTime);
content_cell_1.setCellStyle(contentStyle);

HSSFCell content_cell_2 = contentRow.createCell(1);
content_cell_2.setCellValue(sysMoney);
content_cell_2.setCellStyle(contentStyle);

HSSFCell content_cell_3 = contentRow.createCell(2);
content_cell_3.setCellValue(sysNum);
content_cell_3.setCellStyle(contentStyle);

HSSFCell content_cell_4 = contentRow.createCell(3);
content_cell_4.setCellValue(passagewayMoney);
content_cell_4.setCellStyle(contentStyle);

HSSFCell content_cell_5 = contentRow.createCell(4);
content_cell_5.setCellValue(passagewayNum);
content_cell_5.setCellStyle(contentStyle);

HSSFCell content_cell_6 = contentRow.createCell(5);
content_cell_6.setCellValue(differenceMoney);
content_cell_6.setCellStyle(contentStyle);

HSSFCell content_cell_7 = contentRow.createCell(6);
content_cell_7.setCellValue(differenceNumber);
content_cell_7.setCellStyle(contentStyle);

HSSFCell content_cell_8 = contentRow.createCell(7);
content_cell_8.setCellValue(adjustmentMoney);
content_cell_8.setCellStyle(contentStyle);

HSSFCell content_cell_9 = contentRow.createCell(8);
content_cell_9.setCellValue(adjustmentNumber);
content_cell_9.setCellStyle(contentStyle);

HSSFCell content_cell_10 = contentRow.createCell(9);
content_cell_10.setCellValue(state);
content_cell_10.setCellStyle(contentStyle);

}
return wb;
} catch (Exception e) {
e.getStackTrace();
}

return null;
}

//创建HSSFWorkbook工作薄对象
public static HSSFWorkbook export_day(List<Map<String,Object>> list,String data){
try {
//创建工作薄对象
HSSFWorkbook wb = new HSSFWorkbook();
//创建标题行样式
HSSFCellStyle headStyle = headStyle(wb);
//创建内容行样式
HSSFCellStyle contentStyle = contentStyle(wb);

//创建表
HSSFSheet sheet_1 = wb.createSheet(data+"对账信息");
//设置表的默认列宽
sheet_1.setDefaultColumnWidth(30);

//创建标题行
HSSFRow headRow = sheet_1.createRow(0);
HSSFCell head_cell_1 = headRow.createCell(0); //创建标题行第一列
head_cell_1.setCellValue("对账时间"); //第一列内容
head_cell_1.setCellStyle(headStyle); //将标题行样式添加

HSSFCell head_cell_2 = headRow.createCell(1);
head_cell_2.setCellValue("商户名称");
head_cell_2.setCellStyle(headStyle);

HSSFCell head_cell_3 = headRow.createCell(2);
head_cell_3.setCellValue("系统订单号");
head_cell_3.setCellStyle(headStyle);

HSSFCell head_cell_4 = headRow.createCell(3);
head_cell_4.setCellValue("系统订单金额");
head_cell_4.setCellStyle(headStyle);

HSSFCell head_cell_5 = headRow.createCell(4);
head_cell_5.setCellValue("通道订单号");
head_cell_5.setCellStyle(headStyle);

HSSFCell head_cell_6 = headRow.createCell(5);
head_cell_6.setCellValue("通道订单金额");
head_cell_6.setCellStyle(headStyle);

HSSFCell head_cell_7 = headRow.createCell(6);
head_cell_7.setCellValue("订单状态");
head_cell_7.setCellStyle(headStyle);

HSSFCell head_cell_8 = headRow.createCell(7);
head_cell_8.setCellValue("订单类型");
head_cell_8.setCellStyle(headStyle);

HSSFCell head_cell_9 = headRow.createCell(8);
head_cell_9.setCellValue("交易时间");
head_cell_9.setCellStyle(headStyle);

//为内容行添加数据和样式
for (int i = 1; i <= list.size(); i++) {
HSSFRow contentRow = sheet_1.createRow(i);
Map<String, Object> map=list.get(i-1);
String accountsTime=map.get("accountsTime").toString();
String shopName=map.get("shopName").toString();

String sysNo="";
if(map.get("sysNo")!=null){
sysNo=map.get("sysNo").toString();
}
String sysMoney="";
if(map.get("sysMoney")!=null){
sysMoney=map.get("sysMoney").toString();
}
String passagewayNo="";
if(map.get("passagewayNo")!=null){
passagewayNo=map.get("passagewayNo").toString();
}
String passagewayMoney="";
if(map.get("passagewayMoney")!=null){
passagewayMoney=map.get("passagewayMoney").toString();
}
String orderType="";
if(map.get("orderType")!=null){
int type=Integer.valueOf(map.get("orderType").toString()).intValue();
switch (type) {//0正常1订单缺失2通道缺失3金额不正确
case 0:
orderType="正常";
break;
case 1:
orderType="订单缺失";
break;
case 2:
orderType="通道缺失";
break;
default:
orderType="金额不正确";
break;
}
}
String orderState="";
if(map.get("orderState")!=null){
int state=Integer.valueOf(map.get("orderState").toString()).intValue();
switch (state) {//0正常1未对账2已对帐3已调账
case 0:
orderState="正常";
break;
case 1:
orderState="未对账";
break;
case 2:
orderState="已对帐";
break;
default:
orderState="已调账";
break;
}
}

String transactionTime=map.get("transactionTime").toString();
HSSFCell content_cell_1 = contentRow.createCell(0);
content_cell_1.setCellValue(accountsTime);
content_cell_1.setCellStyle(contentStyle);

HSSFCell content_cell_2 = contentRow.createCell(1);
content_cell_2.setCellValue(shopName);
content_cell_2.setCellStyle(contentStyle);

HSSFCell content_cell_3 = contentRow.createCell(2);
content_cell_3.setCellValue(sysNo);
content_cell_3.setCellStyle(contentStyle);

HSSFCell content_cell_4 = contentRow.createCell(3);
content_cell_4.setCellValue(sysMoney);
content_cell_4.setCellStyle(contentStyle);

HSSFCell content_cell_5 = contentRow.createCell(4);
content_cell_5.setCellValue(passagewayNo);
content_cell_5.setCellStyle(contentStyle);

HSSFCell content_cell_6 = contentRow.createCell(5);
content_cell_6.setCellValue(passagewayMoney);
content_cell_6.setCellStyle(contentStyle);

HSSFCell content_cell_7 = contentRow.createCell(6);
content_cell_7.setCellValue(orderType);
content_cell_7.setCellStyle(contentStyle);

HSSFCell content_cell_8 = contentRow.createCell(7);
content_cell_8.setCellValue(orderState);
content_cell_8.setCellStyle(contentStyle);

HSSFCell content_cell_9 = contentRow.createCell(8);
content_cell_9.setCellValue(transactionTime);
content_cell_9.setCellStyle(contentStyle);

}
return wb;
} catch (Exception e) {
e.getStackTrace();
}

return null;
}
/**
* 创建标题行样式
* @param wb
* @return
*/
public static HSSFCellStyle headStyle(HSSFWorkbook wb){
HSSFCellStyle headStyle = wb.createCellStyle(); //创建样式对象
HSSFFont headFont = wb.createFont(); //创建字体
headFont.setFontName("微软雅黑");
headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headFont.setColor(HSSFFont.COLOR_RED);

headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headStyle.setFont(headFont);
return headStyle;
}

/**
* 创建内容行样式
* @param wb
* @return
*/
public static HSSFCellStyle contentStyle(HSSFWorkbook wb){
HSSFCellStyle contentStyle = wb.createCellStyle();
HSSFFont contentFont = wb.createFont();
contentFont.setFontName("微软雅黑");
contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
contentFont.setColor(HSSFFont.COLOR_NORMAL);

contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
contentStyle.setFont(contentFont);
return contentStyle;
}
}

 

 

/**
* 导出年月的对账单
* @param model
* @param request
* @param response
* @param session
* @param payType
* @param date
* @param type
* @return
*/
@ResponseBody
@RequestMapping(value = "/FINANCIALCENTER/exportByYM")
public ModelAndView exportByYM(Model model, HttpServletRequest request,HttpServletResponse response, HttpSession session,
@RequestParam(value = "payType", required = true) String payType,
@RequestParam(value = "date", required = true) String date,
@RequestParam(value = "type", required = true) String type) {
try {
if(StringUtils.isNotBlank(payType)){
if(payType.equals("1")){
payType="7";
}
if(payType.equals("2")){
payType="6";
}
if(payType.equals("3")){
payType="4";
}
}
Criteria criteria = new Criteria();
criteria.getCondition().put("state", "");
criteria.getCondition().put("type", payType);
SessionUser user = getSessionUser(session);
TUSER tUser = this.tUSERService.queryById(user.getUserId());
Integer agentId=0;
if(tUser.getTYPE().equals(SystemConfig.USER_TYPE_AGENT_T)){
agentId=tUser.getAGENTID();
}
criteria.getCondition().put("agentId",agentId);

List<Map<String,Object>> maps=null;
if(type.equals("1")){//年
criteria.getCondition().put("year", date);
maps=this.adjustService.getOrderByYearAndType(criteria);
}
if(type.equals("2")){//月
criteria.getCondition().put("date", date);
maps=this.adjustService.getOrderByMonthAndType(criteria);
}
if(type.equals("3")){//日
criteria.getCondition().put("day", date);
maps=this.adjustService.getOrderByDayAndType(criteria);
}
if(maps!=null&&maps.size()>0){
//调用方法创建HSSFWorkbook工作簿对象
HSSFWorkbook wb = null;
if(type.equals("3")){//日
wb = ExcelUtil.export_day(maps,date);
}else{
wb = ExcelUtil.export_text(maps,date);
}
try {
//定义导出文件的名称,看不懂的同学可以先行了解一下文件下载
Random r = new Random();
int n2 = r.nextInt(1000);
String name=date+"_"+n2+".xls";
String fileName = new String(name.getBytes("UTF-8"),"ISO-8859-1");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename="+fileName);
OutputStream os = response.getOutputStream();
//将工作薄写入到输出流中
wb.write(os);
os.close();
} catch (Exception e) {
e.getStackTrace();
}
}else{
model.addAttribute(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
model.addAttribute(ErrorMsg.KEY_MESSAGE, date+"该日期没有交易数据");
return new ModelAndView(new MappingJacksonJsonView());
}
} catch (Exception e) {
e.printStackTrace();
model.addAttribute(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
model.addAttribute(ErrorMsg.KEY_MESSAGE, "查詢失败!");
}
return new ModelAndView(new MappingJacksonJsonView());
}

 

 

/**
* 导出excel
*/
exports.exportExcel = function(me){
var year = $cmt_year_details.param.year;
var payType = $cmt_year_details.payType;
var type = 1;

confirmDialog("导出提示", "确定导出Excel么?", {
okAction : function() {
$("#"+$cmt_year_details.page_id).loading(‘show‘);
location.href=$cmt_year_details.config.exportByYM+‘&date=‘+year+‘&payType=‘+payType+‘&type=‘+type;
$("#"+$cmt_year_details.page_id).loading(‘hide‘);
}
});
}

















































































































































































































































































































































































































































以上是关于java poi 提取txt文件内容添加到excel的主要内容,如果未能解决你的问题,请参考以下文章

java 数据导入到exc ,并下载

Java实现通过POI读取Excel文件提取手机号码并去重

java poi技术读取到数据库

提取一个目录下的文件夹的名字

Java POI怎样判断导出的Excel数据是不是正确地保存到本地。考虑网络中断,客户不正确操作等因素。

如何在不提取Java的情况下读取压缩文件的内容