2014-01-18 3 views
1

제발 저의 실제적인 스크립트로 저를 도울 수 있습니까? 내가 선택한 파일을 Excel에서 선택했는데 지금은 어떻게하면 내 목록에 가치를 추가 할 수 있는지 알지 못합니다. 나는 하나의 파일뿐만 아니라 더 많은 파일의 데이터를 수집합니다.어떻게 엑셀 파일에서 읽을 수 있습니까

자료 버튼을 읽기 위해 이벤트를 시작 :

여기에 내 실제로 스크립트입니다. 다음 단계 후

private void next_Click(object sender, EventArgs e) 
{ 
    for (int i = 0; i < UniqueValue.traceToFile.Count; i++) 
    { 
     ReadFromExcel read = new ReadFromExcel(); 
     read.ReadData(UniqueValue.traceToFile[i]); 
    } 
} 

은 이것이다 :

class ReadFromExcel : Config 
{ 
    public void ReadData(string fullpath) 
    { 
     DataSet da = new DataSet(); 
     OleDbDataAdapter adapter = new OleDbDataAdapter(); 
     string cell = "C7"; 
     string name = "List1"; 

     string FileName = fullpath; 
     string _ConnectionString = string.Empty; 
     string _Extension = Path.GetExtension(FileName); 
     // Checking for the extentions, if XLS connect using Jet OleDB 
     if (_Extension.Equals(".xls", StringComparison.CurrentCultureIgnoreCase)) 
     { 
      _ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0};Extended Properties=Excel 8.0", FileName); 
     } 
     // Use ACE OleDb 
     else if (_Extension.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase)) 
     { 
      _ConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", FileName); 
     } 

     OleDbConnection con = new OleDbConnection(_ConnectionString); 
     string strCmd = "SELECT * FROM [" + name + "$" + cell + "]"; 
     OleDbCommand cmd = new OleDbCommand(strCmd, con); 

     try 
     { 
      con.Open(); 
      da.Clear(); 
      adapter.SelectCommand = cmd; 
      adapter.Fill(da); 
     } 

     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 

     finally 
     { 
      con.Close(); 
     } 
    } 
} 

지금 나는이 결과는 내 목록에 기록하는 것을 필요가있다. 모든 파일에서 하나의 셀만 읽습니다. 결과에 대한

목록은 나를 도울 수있는 모든 답변을

public static List<int> money = new List<int>(); 

정말 감사합니다.

답변

0

아래의 코드가 도움이 될 것이라고 생각합니다. 이 예제에서 dt, dt2 및 dt3은 Excel에서 데이터 테이블을 갖는 ReadData 함수의 반환을 고려합니다.

public static List<int> money = new List<int>(); 

protected void Page_Load(object sender, EventArgs e) 
{ 
    DataTable dt = new DataTable(); 
    dt.Columns.Add("ID", typeof(Int32)); 
    dt.Columns.Add("Name"); 
    dt.Rows.Add(); 
    dt.Rows[dt.Rows.Count - 1]["ID"] = 1; 
    dt.Rows[dt.Rows.Count - 1]["Name"] = "Test1"; 



    DataTable dt2 = new DataTable(); 
    dt2.Columns.Add("ID", typeof(Int32)); 
    dt2.Columns.Add("Name"); 
    dt2.Rows.Add(); 
    dt2.Rows[dt2.Rows.Count - 1]["ID"] = 2; 
    dt2.Rows[dt2.Rows.Count - 1]["Name"] = "Test2"; 


    DataTable dt3 = new DataTable(); 
    dt3.Columns.Add("ID", typeof(Int32)); 
    dt3.Columns.Add("Name"); 
    dt3.Rows.Add(); 
    dt3.Rows[dt3.Rows.Count - 1]["ID"] = 3; 
    dt3.Rows[dt3.Rows.Count - 1]["Name"] = "Test2"; 


    money = (from row in dt.AsEnumerable() select Convert.ToInt32(row["ID"])).ToList(); 
    money.AddRange((from row in dt2.AsEnumerable() select Convert.ToInt32(row["ID"])).ToList()); 
    money.AddRange((from row in dt3.AsEnumerable() select Convert.ToInt32(row["ID"])).ToList()); 
} 
+0

나는 그것을 시도 할 것이다. 그것이 나를 위해 일할 수 있다면 나는 당신에게 편지를 씁니다. –

+0

괜찮아요 .. –

+0

당신의 아이디어는 작동하지만 정확히 내가 무엇을 찾고있는 것은 아닙니다. 예를 들어 나는 9 개의 엑셀 파일을 가지고있다. 모든 파일에는 데이터가있는 테이블이 들어 있습니다. 각 파일에서 하나의 셀만 읽습니다. 셀은 모든 파일에서 여전히 "C7"과 동일합니다. 그리고 나는 모든 파일에서 돈 목록에 추가 C7에서 가치가 필요합니다. 그리고 제발 xls 또는 xlsx 파일에 대한 예방을 유지할 수 있습니다. –

-1
Excel.Application excel; 
Excel.Workbook wb; 
Excel.Worksheet sh; 

DataTable dt=new DataTable(); 

excel = new Excel.Application(); 
excel.Visible = true; 
wb = excel.Workbooks.Open("File Path"); 
sh = wb.Sheets.Add(); 
sh.Name = "Data"; 
count = 2; 
sh.Cells[count, "A"].Value2 = "Column Name 1"; 
sh.Cells[count, "B"].Value2 = "Column Name 2"; 
sh.Cells[count, "C"].Value2 = "Column Name 3"; 
sh.Cells[count, "D"].Value2 = "Column Name 4"; 
sh.Cells[count, "E"].Value2 = "Column Name 5"; 
sh.Range["A" + count + "", "E" + count + ""].Font.Size = 12; 
sh.Range["A" + count + "", "E" + count + ""].Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black); 
sh.Range["I2"].WrapText = true; 

try 
{ 
    con.Open(); 
    da.Clear(); 
    adapter.SelectCommand = cmd; 
    adapter.Fill(dt); 
    foreach (DataRow row in dt.Rows) 
    { 
    sh.Cells[count, "A"].Value2 = row["Column Name"].ToString(); 
    sh.Cells[count, "B"].Value2 = row["Column Name"].ToString(); 
    sh.Cells[count, "C"].Value2 = row["Column Name"].ToString(); 
    sh.Cells[count, "D"].Value2 = row["Column Name"].ToString(); 
    sh.Cells[count, "E"].Value2 = row["Column Name"].ToString(); 
    } 
    wb.SaveAs("File Path" + "File Nmae" + ".xls"); 
    excel.Workbooks.Close(); 
    excel.Quit(); 

} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.ToString()); 
} 

finally 
{ 
    con.Close(); 
} 
+0

-1 : 코드를 설명하지 않고 Excel이 컴퓨터에 실제로 설치되어있는 경우에만 작동하며 ASP.NET을 사용하는 경우에는 작동하지 않습니다. –

관련 문제