如何从Excel文件中获取特定列的数据?

问题描述:

嗨我有一个Excel文件,它看起来像这样 ID名年龄phne没有像这样,我有35列.........如何从Excel文件中获取特定列的数据?

我使用的web应用这一在我“已经一个fileupload control,一个按钮和两个文本框

 fileuploadcontrol 
     button 
     textbox1 
     textbox2 

现在,当我上传按钮Excel文件单击它应该阅读完整的文件......... ,当我进入所需的列textbox1我得到那些专栏的细节只有它像这样

textbox1(c1,c4,c5,c30)我sholud只获得那些列的详细信息,并在其他文本框中,如果我输入要保存的位置,它应该保存在该位置,任何人都可以帮助我,我已经完成,直到文件上传和阅读.....所有我需要的是如何评价这些文本框,让我需要的数据

  protected void Button1_Click(object sender, EventArgs e) 
       { 
       if (FileUpload1.HasFile) 
       { 
        if (FileUpload1.PostedFile.ContentType == "application/xlsx") 
        { 
         path = Server.MapPath(".") + "\\inputfiles\\" + Guid.NewGuid() + FileUpload1.FileName; 
         FileUpload1.PostedFile.SaveAs(path); 
         Label1.Text = "File Uploaded Successfully..."; 
         StreamReader reader = new StreamReader(FileUpload1.FileContent); 
         string text = reader.ReadToEnd(); 

         } 
         else 
        Label1.Text = "Upload .xlsx File"; 
         } 
        else 
        Label1.Text = "Upload file"; 

         } 
+0

可以任何指导我笏阅读文件后做 – 2011-12-14 05:21:18

我得到了自己的答案....... TY用于试着助人为乐我米postin我的代码,如果在任何情况下任何需求........

    protected void btns_Click(object sender, EventArgs e) 
         { 
          string path = string.Empty; 
          location = Textbox2.Text; 
          if (fup1.HasFile) 
           { 
            try 
           { 
           path = Server.MapPath(".") + "\\uploadedfiles\\" + Guid.NewGuid() + fup1.FileName; 
          fup1.PostedFile.SaveAs(path); 
         lbl5.Text = "Upload status: File uploaded!"; 
            } 
          catch (Exception ex) 
            { 
             lbl5.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 

             } 
           excelfile(path); 

           } 

         } 

       void excelfile(string path) 
        { 
       string readfile = path; 
       // initialize the Excel Application class 
        Excel.Application app = new Excel.Application(); 

       //Excel.Worksheet NwSheet; 
        Excel.Range ShtRange; 
       // create the workbook object by opening the excel file. 
       Excel.Workbook workBook = app.Workbooks.Open(readfile, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); 
       // Get The Active Worksheet Using Sheet Name Or Active Sheet 
       Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet; 
        int index = 1; 


        System.Text.StringBuilder sb = new StringBuilder(); 
         try 
          { 
         string[] values = Textbox1.Text.Split(new char[] { ',' }); 
        int[] colindexs = new int[values.Length]; 
         for (int i = 0; i < values.Length; i++) 
      { 
       colindexs[i] = Convert.ToInt16(values[i]); 
      } 

      while (((Excel.Range)workSheet.Cells[index, 1]).Value2 != null) 
      { 
       string str = ""; 



       foreach (int col1 in colindexs) 
       { 
        str = str + Convert.ToString(((Excel.Range)workSheet.Cells[index, col1]).Value2) + ","; 
       } 
       str = str.Substring(0, str.Length - 1); 
       sb.Append(str); 
       sb.Append(Environment.NewLine); 
       index++; 
      } 

      Writetofile(sb.ToString()); 

      ShtRange = workSheet.UsedRange;  

     } 
     catch (Exception ex) 
     { 
      app.Quit(); 
      lbl5.Text=ex.Message;  
     } 


    } 

    void Writetofile(string content) 
    { 

     using (System.IO.StreamWriter sw = new System.IO.StreamWriter(location, true)) 
     { 

      sw.Write(content); 
      sw.Flush(); 


     } 
    } 
} 

检查:

Read and Display Data From an Excel File (.xsl or .xlsx) in ASP.NET

,您可以指定需要在选择查询阅读哪些列。

问候

+0

我有点想法,但我怎么做的文本框...我应该得到的文本框中输入的列的数据 – 2011-12-14 05:34:54