...dim dread as SqlDataReader = cmd.ExecuteReader()...StreamWriter ansiWriter = new StreamWriter("test.txt", false, Encoding.GetEncoding(874))dim sb as new System.Text.StringBuilderDo While dread.read() sb.Clear() ... sb.AppendFormat("{0}{1}",dread("x1"),dread("x2")) ... ansiWriter.WriteLine(sb.ToString())End WhileansiWriter.Close();
Date : 2015-07-20 21:24:26
By : watcharop
No. 9
หางอึ่ง
โพสกระทู้ ( 1,348 ) บทความ ( 1 )
กรณีที่ไฟล์คุณเป็น ANSI และเนื้อหาในไฟล์เป็น ภาษาไทย ต้องเปลี่ยน Language for non-Unicode programs เป็น Thai ด้วย
ถ้าไม่รู้วิธีเปลี่ยน ก็ใช้ google ค้นเลยครับ เมื่อกี้ลองค้นแล้ว ก็เจอวิธีเปลี่ยน
Date : 2015-07-20 21:28:15
By : watcharop
No. 10
Aod47
โพสกระทู้ ( 213 ) บทความ ( 0 )
code มาแล้วครับ พร้อม อัพเดทรูปใหม่
Code (C#)
protected void ExportButton_Click(object sender, DirectEventArgs e) { string filename = "K" + Utils.Right(("0000000" + bibId),7) + ".txt"; StringBuilder marc = new StringBuilder(); marc.Append(GetVarField(bibId)); //Download the Text file. Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=" + filename); Response.Charset = ""; Response.ContentType = "application/vnd.text"; //Response.ContentEncoding = System.Text.Encoding.Unicode; Response.Cache.SetCacheability(HttpCacheability.NoCache); System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); Response.Write(marc.ToString()); Response.End(); } private string GetVarField(string bibId) { char cRS = (char)30; // [RS] field terminator char cUS = (char)31; // [US] delimiter char cGS = (char)29; // [GS] record terminator string marc = string.Empty; string directory = string.Empty; string varfield = string.Empty; { // get Variable Fields Value int tagStart = 0; // tag 001.Length + tag 003.Length string queryString = "SELECT * FROM [BibTag_V] WHERE unUsed=0 AND bibId=" + bibId + " ORDER BY tag, tagId"; string connectionString = strConn; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); command.Connection.Open(); command.ExecuteNonQuery(); SqlDataReader dt; dt = command.ExecuteReader(); while (dt.Read()) { string tag = dt["tag"].ToString(); string tagId = dt["tagId"].ToString(); bool isFullstop = bool.Parse(dt["isFullstop"].ToString()); string tagString = string.Empty; directory += tag; /* loop Subfield Value */ string querySf = "SELECT * FROM [BibSubfield_V] WHERE tagId=" + tagId + " ORDER BY tag, tagId, sorder"; using (SqlConnection conSf = new SqlConnection(connectionString)) { SqlCommand commandSf = new SqlCommand(querySf, conSf); commandSf.Connection.Open(); commandSf.ExecuteNonQuery(); SqlDataReader dtSf; dtSf = commandSf.ExecuteReader(); while (dtSf.Read()) { string sCode = dtSf["scode"].ToString(); string sValue = dtSf["svalue"].ToString(); string sPrefix = dtSf["prefixSymbol"].ToString(); string sOrder = dtSf["sorder"].ToString(); if (tag != "008") { if (sOrder == "1") { // add only cUS varfield += cUS; tagString += cUS; } else { // add prefix & cUS if (!string.IsNullOrEmpty(sPrefix)) { varfield += sPrefix + " "; tagString += sPrefix + " "; } varfield += cUS; tagString += cUS; } varfield += sCode; tagString += sCode; } varfield += sValue; tagString += sValue; } { // check last fullstop string ft = Utils.Right(tagString, 1); if (ft != ".") { if (isFullstop) { varfield += "."; tagString += "."; } } } varfield += cRS; tagString += cRS; directory += Utils.Right("0000" + tagString.Length.ToString(), 4); // calculate length of field directory += Utils.Right("00000" + tagStart.ToString(), 5); // calculate starting_chr_position tagStart += int.Parse(tagString.Length.ToString()); tagString = string.Empty; // clear tagString to inital value dtSf.Close(); dtSf = null; } } dt.Close(); dt = null; } } /* get Leader */ int i0004 = 24 + int.Parse(directory.Length.ToString()) + 1 +int.Parse(varfield.Length.ToString()) + 1; // leader.l + directory.l + [RS] + varfield.l + [GS] string l0004 = Utils.Right("00000" + i0004.ToString(), 5); string l05 = "n"; // n == new string l06 = GetTypeOfRecord(bibId); string l07 = GetBibLevel(bibId); string l0811 = " a22"; // fixed value int i1216 = 24 + int.Parse(directory.Length.ToString()) + 1; // leader.Length + directory.Length + [RS] string l1216 = Utils.Right("00000" + i1216.ToString() , 5); string l1719 = " a "; // fixed value string l2023 = "4500"; // fixed value string leader = l0004 + l05 + l06 + l07 + l0811 + l1216 + l1719 + l2023; marc = leader + directory + cRS + varfield + cGS; return marc; }
เพื่อระบุกลับไปที่ ส่วน directory ได้ครับ (ส่วนที่ตีกรอบสีไว้ สีแดงคือ field สีเขียว ความกว้าง field และสีน้ำเงินคือ บอกว่าข้อมูลของ field นี้อยู่ที่ character ที่เท่าไหร่ของไฟล์นี้ ครับ)
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load Dim sLine As String = "ข้าวมันไก่" Response.Clear() Response.ContentEncoding = Encoding.GetEncoding(874) Response.AddHeader("content-disposition", "attachment;filename=test.txt") Response.ContentType = "application/vnd.text" Response.Write(sLine) Response.End() End Sub
Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.
We notice you're using an ad blocker
Without advertising income, we can't keep making this site awesome for you.