C # implement quick reading of txt text data to excel

Source: Internet
Author: User

C # implement quick reading of txt text data to excel

This article mainly introduces C # To quickly read txt text data to excel. This article provides the sample code directly. For more information, see

This feature is pre-implemented today. It transfers the data in txt to an excel table and serves as a data source of matlab. Collect some c # excel operations. The procedure is as follows:

Download a Microsoft. Office. Interop. Excel. dll file and reference it in the project.

Write the following code:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

String path = "c: // date // xyu.txt ";

StreamReader sr = new StreamReader (path );

String strLine = sr. ReadLine ();

Int rowNum = 1;

Object missing = System. Reflection. Missing. Value;

 

ApplicationClass app = new ApplicationClass ();

 

App. Application. Workbooks. Add (true );

 

Workbook book = (Workbook) app. ActiveWorkbook;

Worksheet sheet = (Worksheet) book. ActiveSheet;

While (! String. IsNullOrEmpty (strLine ))

{

String [] tempArr;

TempArr = strLine. Split (',');

For (int k = 1; k <= tempArr. Length; k ++)

{

Sheet. Cells [rowNum, k] = tempArr [k-1];

 

}

StrLine = sr. ReadLine ();

RowNum ++;

 

}

 

// Save the excel file

Book. SaveCopyAs ("D: // source.xls ");

// Close the file

Book. Close (false, missing, missing );

// Exit excel

App. Quit ();

MessageBox. Show ("conversion successful! ");

The above code can implement the function. Because the txt contains 60501 rows of data, the data volume is too large. I estimated that it would take about 2-3 minutes to transfer the code to excel. I want to convert a total of 9 txt files. It takes more than 20 minutes. Such a system is obviously intolerable. Find the information and find that the rang method can increase the speed. It takes about 3-4 seconds to Increase the efficiency by dozens of times. The Code is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

String path = "c: // date // xyu.txt ";

StreamReader sr = new StreamReader (path );

String strLine = sr. ReadLine ();

Int rowNum = 1;

Object missing = System. Reflection. Missing. Value;

 

ApplicationClass app = new ApplicationClass ();

 

App. Application. Workbooks. Add (true );

 

Workbook book = (Workbook) app. ActiveWorkbook;

Worksheet sheet = (Worksheet) book. ActiveSheet;

Range r = sheet. get_Range ("A1", "C1 ");

 

// Obtain the number of rows

 

 

 

 

 

Object [,] objectData = new object [65535, 3];

While (! String. IsNullOrEmpty (strLine ))

{

String [] tempArr;

TempArr = strLine. Split (',');

For (int k = 1; k <= tempArr. Length; k ++)

{

 

ObjectData [rowNum-1, k-1] = tempArr [k-1];

 

}

StrLine = sr. ReadLine ();

RowNum ++;

 

}

R = r. get_Resize (65535, 3 );

R. Value2 = objectData;

R. EntireColumn. AutoFit ();

// Save the excel file

Book. SaveCopyAs ("D: // source.xls ");

// Close the file

Book. Close (false, missing, missing );

// Exit excel

App. Quit ();

MessageBox. Show ("conversion successful! ");

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.