本文将带您了解关于如何在php中上传和解析CSV文件的新内容,同时我们还将为您解释php导入csv的相关知识,另外,我们还将为您提供关于asp.net+js实现无刷新上传解析csv文件的代码、c#–如
本文将带您了解关于如何在php中上传和解析CSV文件的新内容,同时我们还将为您解释php导入csv的相关知识,另外,我们还将为您提供关于asp.net+js 实现无刷新上传解析csv文件的代码、c# – 如何在ASP.NET MVC3中上传和读取CSV文件、c++解析csv文件、java读取csv文件示例分享(java解析csv文件)的实用信息。
本文目录一览:- 如何在php中上传和解析CSV文件(php导入csv)
- asp.net+js 实现无刷新上传解析csv文件的代码
- c# – 如何在ASP.NET MVC3中上传和读取CSV文件
- c++解析csv文件
- java读取csv文件示例分享(java解析csv文件)
如何在php中上传和解析CSV文件(php导入csv)
我想用php上传一个csv文件。上传文件后,我想显示CSV文件的数据。我想举一个如何完成这项任务的例子。
答案1
小编典典尽管您可以轻松找到如何使用php处理文件上传的教程,并且有一些功能(手动)可以处理CSV,但是我将发布一些代码,因为几天前我从事一个项目,其中包括一些代码,您可以采用…
HTML:
<table width="600"><form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data"><tr><td width="20%">Select file</td><td width="80%"><input type="file" name="file" id="file" /></td></tr><tr><td>Submit</td><td><input type="submit" name="submit" /></td></tr></form></table>
PHP:
if ( isset($_POST["submit"]) ) { if ( isset($_FILES["file"])) { //if there was an error uploading the file if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { //Print file details echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; //if file already exists if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { //Store file in directory "upload" with the name of "uploaded_file.txt" $storagename = "uploaded_file.txt"; move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $storagename); echo "Stored in: " . "upload/" . $_FILES["file"]["name"] . "<br />"; } } } else { echo "No file selected <br />"; }}
我知道必须有一种更简单的方法来执行此操作,但是我读取了CSV文件并将每个记录的单个单元格存储在二维数组中。
if ( isset($storagename) && $file = fopen( "upload/" . $storagename , r ) ) { echo "File opened.<br />"; $firstline = fgets ($file, 4096 ); //Gets the number of fields, in CSV-files the names of the fields are mostly given in the first line $num = strlen($firstline) - strlen(str_replace(";", "", $firstline)); //save the different fields of the firstline in an array called fields $fields = array(); $fields = explode( ";", $firstline, ($num+1) ); $line = array(); $i = 0; //CSV: one line is one record and the cells/fields are seperated by ";" //so $dsatz is an two dimensional array saving the records like this: $dsatz[number of record][number of cell] while ( $line[$i] = fgets ($file, 4096) ) { $dsatz[$i] = array(); $dsatz[$i] = explode( ";", $line[$i], ($num+1) ); $i++; } echo "<table>"; echo "<tr>"; for ( $k = 0; $k != ($num+1); $k++ ) { echo "<td>" . $fields[$k] . "</td>"; } echo "</tr>"; foreach ($dsatz as $key => $number) { //new table row for every record echo "<tr>"; foreach ($number as $k => $content) { //new table cell for every field of the record echo "<td>" . $content . "</td>"; } } echo "</table>";}
因此,我希望这会有所帮助,这只是一小段代码,并且我尚未对其进行测试,因为我使用的代码略有不同。评论应解释一切。
asp.net+js 实现无刷新上传解析csv文件的代码
前阵子工作中用到,贴上代码,仅保留上传有关的代码,发现code其实很少。上传页面html/js
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script type="text/javascript">
function FinishUpload(filePath) {
document.getElementById("uploadForm").reset();
if (!filePath) {
alert("Import Failed!");
}
else {
alert("Imported Successfully to " + decodeURIComponent(filePath) + "!");
}
}
function UploadFile() {
var arr = document.getElementById("txtFile").value.split('.');
var fileType = arr[arr.length - 1];
if (fileType.toLowerCase().indexOf("csv") < 0) {
document.getElementById("uploadForm").reset();
alert("Please select a csv file.");
return false;
}
document.getElementById("uploadForm").encoding = "multipart/form-data";
document.getElementById("uploadForm").submit();
}
function ResetFile(file) {
var tmpForm = document.createElement('form');
file.parentNode.insertBefore(tmpForm,file);
tmpForm.appendChild(file);
tmpForm.reset();
tmpForm.removeNode(false);
}
</script>
</head>
<body>
<form id="uploadForm" name="uploadForm" action="Upload.ashx" method="post" target="hidIframe" enctype="multipart/form-data">
<table cellpadding='0' cellspacing='0'border="0" >
<tr>
<td>
<input id="txtFile" name="txtFile" type="file"/>
<iframe name="hidIframe" id="hidIframe"https://www.jb51.cc/tag/dis/" target="_blank">display:none;" ></iframe>
</td>
</tr>
<tr>
<td>
<input type="button" id="btnImportOK" value="Upload" onclick="UploadFile();" />
<input type="button" id="btnImportCancel" onclick="ResetFile(document.getElementById('txtFile'))" value="Reset"/>
</td>
</tr>
</table>
</form>
</body>
</html>
处理文件上传的c#
if (context.Request.Files.Count > 0)
{
HttpPostedFile file = context.Request.Files[0];
if (file.ContentLength > 0)
{
string title = string.Empty;
title = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(file.FileName);
string path = "./Upload/" + title;
path = System.Web.HttpContext.Current.Server.MapPath(path);
file.SaveAs(path);
context.Response.Write("<script>window.parent.FinishUpload('" + HttpUtility.UrlEncode(path) + "');</script>");
}
}
else
{
context.Response.Write("<script>window.parent.FinishUpload('');</script>");
}