Hướng dẫn how read excel file in c# using microsoft office interop excel? - cách đọc tệp excel trong c# bằng microsoft office interop excel?

Vì vậy, có vẻ như giảng viên đã bao gồm tệp .xlsx như là phương tiện để hiển thị ví dụ về tệp được phân định tab. Đây là hoàn thành:

#include "stdafx.h"
#include 
#include "string.h"

void printService[char *type, char *reg, char serviceType, char *month];

void main[]
{ 
    char months[12][12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    char line[201];
    char type[41], reg[41];
    char service[12];
    int i;

    FILE *ptr;  

    /*
    ptr = fopen[ "c:\\path\\to\\file.txt", "r" ];

   if [ptr == NULL ] 
   {
      printf[ "File could not be opened\n" ];
   } 
   else 
   { 
        puts["File Contents"];
        while[!feof[ptr]]
        {
            fgets[line,200,ptr];
            puts[line];
        }

      fclose[ ptr ]; 
   } 
   */

   ptr = fopen[ "c:\\path\\to\\file.txt", "r" ];

   if [ptr == NULL ] 
   {
      printf[ "File could not be opened\n" ];
   } 
   else 
   { 
            fgets[line,200,ptr];
            fgets[line,200,ptr];
            while[strcmp[type,"END"]]
            {
                type[0] = '\0';
                fscanf[ptr, "%s\t%s\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\n",type,reg,&service[0],&service[1],&service[2],&service[3],
                &service[4],&service[5],&service[6],&service[7],&service[8],&service[9],&service[10],&service[11]];

                if[strcmp[type,"END"]]
                {
                for[i=0;i COM tab

Tiếp theo, bạn sẽ cần tạo tài liệu tham khảo cho từng đối tượng COM được truy cập. Mỗi tài liệu tham khảo phải được giữ để thoát khỏi ứng dụng một cách hiệu quả khi hoàn thành.

//Create COM Objects. Create a COM object for everything that is referenced
Excel.Application xlApp = new Excel.Application[];
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open[@"sandbox_test.xlsx"];
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;

Sau đó, bạn có thể đọc từ tờ, hãy nhớ rằng việc lập chỉ mục trong Excel không dựa trên 0. Điều này chỉ đọc các ô và in lại chúng giống như trong tập tin.

//iterate over the rows and columns and print to the console as it appears in the file
//excel is not zero based!!
for [int i = 1; i 

Bài Viết Liên Quan

Chủ Đề