2011-12-14 117 views
0

嗨我有一個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

回答

0

我得到了自己的答案....... 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(); 


     } 
    } 
} 
相關問題