大家好,
附加代码当前代码读入我的EXCEL文件(示例如下所示)并创建一个TEXT文件(如下所示)。
名称
Name
ID
ID
城市
City
ID
ID
型号
model
尺寸
size
加载
load
id
id
型号
model
reg
reg
主要
main
前
ex
Billy
1
达拉斯
234
GWX
20
是
3423
tt
556-6
1000
45
Bob
2
芝加哥
555
GYZ
30
否
455
dgx
56-6
2000
50
TEXT:
TEXT:
一般 F2 F3 Car F5 F6 &NBSP ; F7 船&NBSP ; F9 F10 F11 F12
General F2 F3 Car F5 F6 F7 Boat F9 F10 F11 F12
名称 城市 ID model size load id model reg   ; main
Name City ID model size load id model reg main
比利 1   ; 达拉斯 234 GWX   ; 20 是&NBSP ; 3423 tt 556-6 1000 45
Billy 1 Dallas 234 GWX 20 yes 3423 tt 556-6 1000 45
Bob 2 ; 芝加哥 555 GYZ 30 &NBSP ; 没有 455 dgx 56-6 2000 ; 50
Bob 2 Chicago 555 GYZ 30 no 455 dgx 56-6 2000 50
- 我希望我的代码能做的是:
1.基于MAIN类别(GENERAL,CAR,BOAT),SUB类别(INFO,MAIN,SUB…)为每个主要类别和列标题( Name,ID,City…)每个SUB类别,在特定 value -decoration:在文本文件中加下"> START / END 位置。
1. Based on the MAIN Category (GENERAL,CAR,BOAT) , SUB Category (INFO,MAIN,SUB…) for each MAIN Category AND column heading (Name, ID, City…) for each SUB Category, place the value of the column in specific START/END position in the text file.
EX。对于Car / Main / ID–开始位置为30,结束位置为40(因此它看起来像"234 ; …剩余7个空格”)
EX. For Car/Main/ID – the Start position is 30 and End position is 40 (therefore it would look like “234 …with 7 spaces left over”)
对于Car / Main / modwl–起始位置为10,结束位置为25(因此它看起来像"GWX…。有12个空格空白”"
For Car/Main/modwl – the start position is 10 and End Position is 25 (therefore it would look like “GWX …. With 12 spaces blank”)
因此:对于第一行:Billy,1,Dallas等都会在文本文件中具有特定的START / END位置
Therefore: for the first row: Billy,1,Dallas etc. would all have specific START/END positions in a text file
非常感谢任何帮助!
如果需要进一步说明,请告诉我!我知道这可能会令人困惑!!!
非常感谢你!
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OleDb; using System.Data; using System.IO; namespace Excel2010Transform { class Transform_OleDb { static void Main(string[] args) { //The connection string to the excel file String path = @"C:\AAATemp\NameID.xlsx"; String connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;"; //The connection to that file OleDbConnection conn = new OleDbConnection(connStr); //The query string strSQL = "SELECT * FROM [Sheet1$]"; //The command OleDbCommand cmd = new OleDbCommand(/*The query*/strSQL, /*The connection*/conn); DataTable dt = new DataTable(); conn.Open(); try { OleDbDataReader dr1 = cmd.ExecuteReader(); StreamWriter sw = new StreamWriter("C:\\AAAtemp\\export.txt"); if (dr1.Read()) { dt.Load(dr1); } int iColCount = dt.Columns.Count; for (int i = 0; i < iColCount; i++) { sw.Write(dt.Columns[i]); if (i < iColCount - 1) { sw.Write("\t"); } } sw.Write(sw.NewLine); // Now write all the rows. foreach (DataRow dr in dt.Rows) { for (int i = 0; i < iColCount; i++) { if (!Convert.IsDBNull(dr[i])) { sw.Write(dr[i].ToString()); } if (i < iColCount - 1) { sw.Write("\t"); } } sw.Write(sw.NewLine); } sw.Close(); Console.WriteLine("File is saved"); } catch (OleDbException caught) { Console.WriteLine(caught.Message); } finally { conn.Close(); } Console.Read(); } } }
推荐答案
我认为通过以下信息,您可以自己完成代码,
I think with this information below you will able to finish your code by yourself,
很抱歉,如果您已经知道这些方法,但您似乎需要有关字符串操作的帮助吗?
Sorry if you already know this methods but You seem you need help about string operations ?
如果您想要写一个长度恰好为20的名称并在末尾放置空格,
if you want to write a name with exactly 20 length and put spaces at the end,
代码是这样的
strPerson.PadRight(20,'');
strPerson.PadRight(20,' ');
有PadLeft ...检查这个页面它是非常容易理解的,http://www.dotnetperls/padright
also there is PadLeft... check this page its very understandable , www.dotnetperls/padright
你需要学习SubString方法,它得到一个字符串Sub String
you need to learn SubString method, it gets a strings Sub String
喜欢"EsrefDURNA".SubString(1,3)会给你"sre"非常简单的方法。
like "EsrefDURNA".SubString(1,3) will give you "sre" its very simple method.
- 如果您遇到问题,请更清楚,更具体地解释您的问题。
-- if you keep having problem, please explain your problem more clearly and more specific.
更多推荐
C#写入TEXT文件
发布评论