C # CSV file Import Export

Source: Internet
Author: User

1. Export of asp.net:

<pre name= "code" class= "CSharp" >stringbuilder DATA = new StringBuilder ("");

Response.Clear ();
Response.ContentType = "Text/plain; Charset=shift_jis ";
Response.AddHeader ("Content-disposition", "attachment; Filename= "+ filename +". csv ");
...  Obtain data data from DB
. Append (...);	Write data to the Data,csv file to split the

Response.Write (data. ToString ());
Response.End ();

Export of 2.WinForm:
...//Get the Save path ...
//Get data from db ...
//with 1, will be "," split the StringBuilder
StreamWriter sw = new StreamWriter ( Exportpath, False, Encoding.GetEncoding ("Shift_JIS"));
Sw. Write (Stringdata.tostring ());

Import of 3.asp.net:
HtmlInputFile txthtmlinputfile = txtfile; Txtfile is the document obtained in the input control in the picture
System.IO.Stream instream = txtHtmlInputFile.PostedFile.InputStream;

DataStream read
byte[] mstreambackup = new Byte[instream.length];
Instream.read (mstreambackup, 0, Int.) Parse (InStream.Length.ToString ()));
The stream
MemoryStream chkstream = new MemoryStream (mstreambackup) used for check;

System.Text.Encoding enc = System.Text.Encoding.Default;
TextFieldParser parser = new TextFieldParser (Chkstream, enc);
Parser. TextFieldType = fieldtype.delimited;
Parser. Setdelimitens (",");
Parser. Trimwhitespace = false;

while (!parser. EndOfData) {
	String strorgtext = parser. ReadFields ();	Read one row
	string[] Strarytext = new String[strorgtext.length];
	int i=0;
	foreach (String field in Strorgtext) {
		strargtext[i] = field;
		i++;
	}
	...	Perform various check
	...	Insert data into DB
}

parser. Close ();
parser = null;

4.WinForm Import:
private sctteam[] impteamary;//Save with Import data Private struct sctteam{public String itema;
//b ...//c}//impteamary write CSV data//Use OpenFileDialog to get csv file String strFileName = openfiledialog1.filename;
StreamReader sr = new StreamReader (strFileName, System.Text.EncodingDefault); String StrText = Sr.	ReadToEnd (); Read the last If (strtext.length <= 0 | | | strtext.substring (0, 1)! "\") {...//Read failed message} Sr.
Close ();	Strtext.replace ("R", "");
Delete line Wrap string[] strtextary = strtext.split (' \ n ');
if (Strtextary.length < 1) {...//read failure message}//definition and memory Impteamary = new Sctteam[strtextary.length];	for (int i=0; i<strtextary.length; i++) {Impteamary[i] = new Sctteam ();//Ensure memory}//Get data int j=0; The data array index for (int ii=0; ii<=strtextary.length-1; ii++) {if (strtextary[ii). ToString (). Trim ().
	Length = = 0) continue; Each project obtains string[] Straryfield = Strtextary[ii].
	Split (', '); Impteamary[j].itema = straryfield[0]. ToString ().
	Replace ("\", "");
...//b.//c j + +; }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.