如何用php向txt写入数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用php向txt写入数据相关的知识,希望对你有一定的参考价值。
一个php文件向txt写入数据,不要覆盖以前的内容也不用转行以 | 分隔开两个数据.
/*先取出*/$string = file_get_contents("1.txt");
$newstring; // 新数据
if (empty($string))
$string = $newstring;
else
$string .= '|' . $newstring;
file_put_contents("1.txt", $string); 参考技术A 路过。。。
如何用java向指定的Excel中写入数据
jxl,可以实现,开源的jar包很多,目前我使用的是jxlSimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String id =req.getParameter("id");
String fileName =req.getParameter("fileName");
File f = new File(req.getSession().getServletContext().getRealPath("/printTemplateExcel/"+fileName));
WritableWorkbook wwb=null;
WritableSheet wws=null;
FileOutputStream out =null;
Label label = null;
Workbook wb=null;
try
FesOrders fesOrders =new FesOrders();
fesOrders.setId(Long.parseLong(id));
List<FesOrders> fesOrdersList = fesOrderService.findList(fesOrders, (SysUser)req.getSession().getAttribute(SESSION_USER));
for(FesOrders a : fesOrdersList)
System.out.println(a.getId());
WritableFont headerFont =new WritableFont(WritableFont.createFont("新宋体"), 10, WritableFont.BOLD, false,UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat dataFormat=new WritableCellFormat (headerFont);
dataFormat.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);//设置边框
WritableCellFormat wf = new WritableCellFormat(dataFormat);
wb = Workbook.getWorkbook(f);
out = new FileOutputStream(new File(req.getSession().getServletContext().getRealPath("/printExcel/"+fileName)));
wwb = Workbook.createWorkbook(out, wb);
wws = wwb.getSheet("出货单");
String date=sdf.format(new Date());
for(int i=0;i<wws.getRows();i++)
for(int j=0;j<wws.getColumns();j++)
Cell cell = wws.getCell(j, i);
if(cell.getContents().trim().indexOf("$year")>0)
label = (Label)cell;
label.setString(cell.getContents().replace("$year", date.subSequence(0, 4)));
if(cell.getContents().trim().indexOf("$mouth")>0)
label = (Label)cell;
label.setString(cell.getContents().replace("$mouth", date.substring(5, 7)));
if(cell.getContents().trim().indexOf("$day")>0)
label = (Label)cell;
label.setString(cell.getContents().replace("$day", date.substring(8, 10)));
System.out.println(fesOrdersList.size());
for(int i=0;i<fesOrdersList.size();i++)
FesOrders fesOrder= fesOrdersList.get(i);
List<FesSendOrders> fesSendOrders=fesOrder.getFesSendOrderses();
wws.addCell(new Label(0,i+2,i+1+"",wf));
wws.addCell(new Label(1,i+2,fesOrder.getSysUser().getCnName(),wf));
wws.addCell(new Label(2,i+2,fesOrder.getInnerSendNo(),wf));
wws.addCell(new Label(3,i+2,fesSendOrders.get(0).getFesTransportModes().getName(),wf));
if(fesSendOrders.get(0).getOpWeight()!=null)
wws.addCell(new Label(4,i+2,fesSendOrders.get(0).getOpWeight().toString(),wf));
wws.addCell(new Label(5,i+2,"",wf));
wws.addCell(new Label(6,i+2,fesSendOrders.get(0).getFesAddresseesBySenderId().getAddress(),wf));
wws.addCell(new Label(7,i+2,"",wf));
wws.addCell(new Label(8,i+2,fesOrder.getOrderNo(),wf));
wws.addCell(new Label(9,i+2,"",wf));
wwb.write();
wwb.close();
out.flush();
out.close();
download(req,res,fileName);
这个是我写的简单的导出excel 参考技术A 用apache的POI包可以实现 参考技术B 如果没用中间件的话,应该是有一些开源的jar包,那些jar包里面就提供了这些方法比较简单,从网上找一些 参考技术C 我们项目里用的 供你参考,没写全,你可以自己百度下
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
/**
* 导出excel
* @param reportParams 导出excel列名标示
* @param list导出excel值
* @param headersexcel头
* @param reportName·excel的sheet名
* @param response
* @param startRow从哪一行开始放list值
* @param startCol 从哪一列开始放list值
* @return
*/
public boolean report(ReportBean rb, List list, XlsHeaderBean[] headers, String reportName, HttpServletResponse response, int startRow, int startCol, HttpServletRequest request)
WritableWorkbook wwb = null;
OutputStream os;
boolean flag = true;
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String strDate = format.format(date);
try
// 用Workbook类的工厂方法创建工作薄(Workbook)对象
response.setContentType("application/x-msdownload");
String sheetName = "report";
sheetName = sheetName.replaceAll(":", "").replaceAll("[)]", "")
.replaceAll("[(]", "");
// 这里解释一下
// attachment; 这个代表要下载的,如果去掉就编程直接打开了
// filename是文件名,另存为或者下载时,为默认的文件名
response.addHeader("Content-Disposition", "attachment; filename="
+ new String(sheetName.getBytes("UTF-8"), "ISO-8859-1")+ strDate
+ ".xls");
os = response.getOutputStream();
wwb = Workbook.createWorkbook(os);
catch (IOException e)
e.printStackTrace();
return flag = false;
if (wwb != null)
// 创建一个可写入的工作表
// Workbook的createSheet方法两个参数,1名称,2位置
WritableSheet ws = wwb.createSheet(reportName, 0);
// 下面开始添加单元格
// 导出excel
try
Label labelC = null;
for(int j = 0; j < headers.length; j++)
if(headers[j].isUnion())
ws.mergeCells(headers[j].getCol(), headers[j].getRow(), headers[j].getCol()+headers[j].getColLength(), headers[j].getRow()+headers[j].getRowLength());
labelC = new Label(headers[j].getCol(), headers[j].getRow(), headers[j].getValue());
ws.addCell(labelC);
catch (RowsExceededException e)
e.printStackTrace();
catch (WriteException e)
e.printStackTrace();
String value = null;
for (int i = 0; i < list.size(); i++)
Map values = (Map)list.get(i);
for (int j = 0; j < rb.getReportParams().length; j++)
// 这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行
Label labelC;
if(values.get(rb.getReportParams()[j]) == null)
value = "";
else
if(values.get(rb.getReportParams()[j]) instanceof java.util.Date)
value = format.format(values.get(rb.getReportParams()[j]));
else if(values.get(rb.getReportParams()[j]) instanceof java.math.BigDecimal)
value = values.get(rb.getReportParams()[j]).toString();
else
value = values.get(rb.getReportParams()[j]).toString();
labelC = new Label(j+startCol, i + startRow, value);
try
// 将生成的单元格添加到工作表中
ws.addCell(labelC);
catch (RowsExceededException e)
e.printStackTrace();
return flag = false;
catch (WriteException e)
e.printStackTrace();
return flag = false;
if(rb.isHasImg())
String rootPath = this.getServletContext().getRealPath("savefiles");
String imgPath = rb.getImgPath();
if(rootPath != null && rootPath.compareTo("") != 0 && imgPath != null && imgPath.compareTo("") !=0)
String[] strs = imgPath.split("\\/");
String imgName = strs[strs.length-1];
File file = new File(rootPath + File.separator + imgName);
if(file.exists())
WritableImage wi = new WritableImage(0, startRow + list.size() + 2,12,20, file);
ws.addImage(wi);
try
// 从内存中写入文件中
wwb.write();
wwb.close();
return flag;
catch (IOException e)
e.printStackTrace();
return flag = false;
catch (WriteException e)
e.printStackTrace();
return flag = false;
return flag;
以上是关于如何用php向txt写入数据的主要内容,如果未能解决你的问题,请参考以下文章
如何用java写入txt,每次运行都覆盖前一次的内容(多行数据)