Read Excel, read text files

Source: Internet
Author: User
Tags switch case

Background:

The leader did not know what to do in the project, the day before yesterday gave us a 200 line above the table, let us put the contents, with switch case written out.

I and colleagues division of Labor, probably wrote more than 100, at least half an hour or more, after writing, Exchange check, think, just finished more than 100 switch case, but also to check more than 100,

Dizzy, really dizzy, sure enough, both of them have errors, completed this any use for 2 hours or so.

Today the leadership is not, yesterday gave us a task, another such form, with Swtich case written out.

We were drunk, okay, but we didn't say anything. After coming to work this morning, I will finish the other tasks first. Then do this.

I see, this is not more than 200 lines, a full 700,800 lines. means to write more than 700 case:break;

I think, ligatures with check, how also got small half a day, so write down not a way.

So I consulted with colleagues, asked if he could write a program directly generated. He said yes, but there is no way to read the files in CHM format.

So I put the CHM format table, copied, pasted into, Excel. He said OK, this is good to do, but I am a rookie, nothing, he said let me write, said this task to me a challenge, he can give me ideas, and then I think, according to their own way, will excel into a text file (with a comma splitter), the file read into the C # program. Using string concatenation, very simple, in order to write over more than 700 case.

Later looked at my colleague's writing, he is directly read Excel, so I tried it myself, wrote a direct from the Excel program, in fact, are very simple.

Pay the following code:

Static voidMain (string[] args) {            //readtxt ();Readexcel (); }        Private Static voidReadexcel () {StringBuilder sb=NewStringBuilder (); stringStringcase =@"Case {0}: Code = "" {1} "";                                                          Info = "" {2} "";                                    Break "; stringConnStr ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:/tmp.xls; Extended properties= ' Excel 8.0; Hdr=no;imex=1 ';";//Connection StringOleDbConnection Conn=NewOleDbConnection (CONNSTR);            Conn.Open (); stringsql =string. Format ("SELECT * FROM [{0}$]","Sheet1");//Sheet1 The name of the worksheetDataSet ds =NewDataSet (); OleDbDataAdapter da=NewOleDbDataAdapter (SQL, Conn); Da.   Fill (DS); //read data is populated into a dataset            foreach(DataRow rowinchDs. tables[0]. Rows)//using the table loop            {                stringCasetmp =""; Casetmp= String.Format (Stringcase, row[1]. ToString (), row[0]. ToString (), row[2].                ToString ()); Sb.            Append (CASETMP); } Console.WriteLine (sb.)            ToString ());        Console.readkey (); }        Private Static voidReadtxt () {StringBuilder sb=NewStringBuilder (); stringStringcase =@"Case {0}: Code = "" {1} "";                                                          Info = "" {2} "";                                    Break "; string[] lines = File.ReadAllLines ("F:\\tmp.csv", Encoding.default);//Read all Rows            foreach(stringLineinchLines//to make a foreach loop on a row            {                stringCasetmp =""; string[] values = line. Split (',');//in-line data usage, sub-gridCasetmp = String.Format (Stringcase, values[1], values[0], values[2]); Sb.            Append (CASETMP); } file.writealltext ("F:\\tmp.txt", SB. ToString ());//writes the completed string to the file//Console.WriteLine (sb.) ToString ());Console.readkey (); }

Take whatever you need.

Read Excel, read text files

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.