代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
CSV导入导出
作者:
Dezai.CN
/ 发布于
2012/8/17
/
601
CSV导入导出
<div>导入代码,从csv文件得到datatable</div> <div> /// <summary> /// Get Data From Csv File /// (Through StreamReader) /// </summary> /// <returns></returns> private bool GetData(Stream inputStream, out string errMessage, out DataTable dtFile) { errMessage = String.Empty; dtFile = new DataTable();</div> <div> //this.FileUploadImport.PostedFile.InputStream.CanSeek; //StreamReader sr = new StreamReader(this.FileUploadImport.PostedFile.InputStream); //update by hwx 11/12/2010 StreamReader sr = new StreamReader(inputStream); int iColumnCount = 19;//the column count in the file int iRow = 1;// the row number is being read int iColumn = 0;// the column number is being read string strStandardTitle = string.Empty; // the title as it is</div> <div> //read title row string strTitle = sr.ReadLine();</div> <div> //string[] strRowTitle = strTitle.Split(new char[] { ',' }); string[] strRowTitle = new string[iColumnCount]; string strCharTitle; int iCellNumberTitle = 0; bool blnQuoteTitle = false; for (int i = 0; i < strTitle.Length; i++) { strCharTitle = strTitle.Substring(i, 1); if (strCharTitle == ",")// "," is the seperation symbol of csv file, { if (!blnQuoteTitle) { iCellNumberTitle++;// out of the "" range, "," is the seperation symbol if (iCellNumberTitle >= iColumnCount)// too many column in this line { break; } } else { strRowTitle[iCellNumberTitle] += strCharTitle; } } else if (strCharTitle == "\"")// "\"" is the transfer symbol of csv file, { blnQuoteTitle = !blnQuoteTitle; if (blnQuoteTitle && i > 0 && strTitle.Substring(i - 1, 1) == "\"")//in the "" range and there is an transfer symbol before { strRowTitle[iCellNumberTitle] += strCharTitle; } } else { strRowTitle[iCellNumberTitle] += strCharTitle; } } //read the content if (strRowTitle.Length == iColumnCount) { foreach (string strCell in strRowTitle) { iColumn++; if (strCell.Trim() != string.Empty) { dtFile.Columns.Add(strCell);//add new column with name to the data table</div> <div> } else //file error:blank title {</div> <div> errMessage += "The cell " + iColumn.ToString() + " is blank in the header row.\r\n"; } } if (dtFile.Columns.Count == iColumnCount)//make sure that no blank header or error header { //read content row string strLine; while (!sr.EndOfStream) { iRow++; iColumn = 0; DataRow dr = dtFile.NewRow(); strLine = sr.ReadLine();</div> <div> //read csv file line by line string[] strRow = new string[iColumnCount]; string strChar; int iCellNumber = 0; bool blnQuote = false;//whether in the "" range for (int i = 0; i < strLine.Length; i++) { strChar = strLine.Substring(i, 1); if (strChar == ",")// "," is the seperation symbol of csv file, { if (!blnQuote) { iCellNumber++;// out of the "" range, "," is the seperation symbol if (iCellNumber >= iColumnCount)// too many column in this line { break; } } else { strRow[iCellNumber] += strChar; } } else if (strChar == "\"")// "\"" is the transfer symbol of csv file, { blnQuote = !blnQuote; if (blnQuote && i > 0 && strLine.Substring(i - 1, 1) == "\"")//in the "" range and there is an transfer symbol before { strRow[iCellNumber] += strChar; } } else { strRow[iCellNumber] += strChar; } } if (iCellNumber + 1 == iColumnCount) { foreach (string strCell in strRow) { iColumn++; if (strCell != null && strCell.Trim() != string.Empty) { dr[strRowTitle[iColumn - 1]] = strCell.Trim(); } else//file error:blank cell { dr[strRowTitle[iColumn - 1]] = String.Empty; //errMessage += "The column \"" + strRowTitle[iColumn - 1] + "\" is blank in row " + iRow.ToString() + ".\r\n"; } } } else// file error:the column count of current row do not equal to title's { errMessage += "There are more or less cells than title row in the row " + iRow.ToString() + ".\r\n"; } dtFile.Rows.Add(dr); } } } else //file error:the count of columns in the file don't equal it should be { errMessage += "There are an incorrect number of columns in the header row compared to the template file.\r\n"; } sr.Close(); sr.Dispose();</div> <div> errMessage = errMessage.Replace("\r\n", ""); return errMessage == String.Empty ? true : false; } <div> /// <summary> /// Get dataset from csv file. /// </summary> /// <param name="filepath"></param> /// <param name="filename"></param> /// <returns>Data Set</returns> private DataSet GetDatasetFromCsv(string filepath, string filename) { string strconn = @"driver={microsoft text driver (*.txt; *.csv)};dbq="; strconn += filepath; //filepath, for example: c:\ strconn += ";extensions=asc,csv,tab,txt;"; OdbcConnection objconn = new OdbcConnection(strconn); DataSet dscsv = new DataSet(); try { string strsql = "select * from " + filename; //filename, for example: 1.csv OdbcDataAdapter odbccsvdataadapter = new OdbcDataAdapter(strsql, objconn);</div> <div> odbccsvdataadapter.Fill(dscsv); return dscsv; } catch (Exception ex) { throw ex; } }
试试其它关键字
CSV导入导出
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3