EXCELREADER.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Data;
  5. using System.Data.Odbc;
  6. public class EXCELREADER
  7. {
  8. // our odbc connector
  9. OdbcConnection odbcCon = null;
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. /// <param name="filepath"></param>
  14. public EXCELREADER(string filepath)
  15. {
  16. string connect = "Driver={Microsoft Excel Driver (*.xls)}; DriverId=790; Dbq=" + filepath + ";";
  17. Debug.Log(connect);
  18. odbcCon = new OdbcConnection(connect);
  19. }
  20. public void Open()
  21. {
  22. odbcCon.Open();
  23. }
  24. public void Close()
  25. {
  26. odbcCon.Close();
  27. }
  28. OdbcCommand odbcCmd;
  29. public void GetSheet(String sheetname)
  30. {
  31. string query = "SELECT * FROM [" + sheetname + "$]";
  32. if (odbcCmd != null)
  33. odbcCmd = null;
  34. odbcCmd = new OdbcCommand(query, odbcCon);
  35. }
  36. public DataTable GetDataFrom()
  37. {
  38. // table to hold the data
  39. DataTable dtYourData = new DataTable("YourData");
  40. // lets use a datareader to fill that table!
  41. OdbcDataReader rData = odbcCmd.ExecuteReader();
  42. // now lets blast that into the table by sheer man power!
  43. dtYourData.Load(rData);
  44. //// Use a DataTable object's DataColumnCollection.
  45. //DataColumnCollection columns = dtYourData.Columns;
  46. //// Print the ColumnName and DataType for each column.
  47. //foreach (DataColumn column in columns)
  48. //{
  49. // Debug.Log(column.ColumnName);
  50. // Debug.Log(column.DataType);
  51. //}
  52. // close that reader!
  53. rData.Close();
  54. return dtYourData;
  55. }
  56. public void readXLS( string filetoread)
  57. {
  58. // Must be saved as excel 2003 workbook, not 2007, mono issue really
  59. string con = "Driver={Microsoft Excel Driver (*.xls)}; DriverId=790; Dbq="+filetoread+";";
  60. Debug.Log(con);
  61. //string yourQuery = "SELECT * FROM [Sheet1$]";
  62. string yourQuery = "SELECT * FROM [ºìÂÞº£°¶_Õ½¶·µØͼ1_MAP$]";
  63. // our odbc connector
  64. OdbcConnection oCon = new OdbcConnection(con);
  65. // our command object
  66. OdbcCommand oCmd = new OdbcCommand(yourQuery, oCon);
  67. // table to hold the data
  68. DataTable dtYourData = new DataTable("YourData");
  69. // open the connection
  70. oCon.Open();
  71. // lets use a datareader to fill that table!
  72. OdbcDataReader rData = oCmd.ExecuteReader();
  73. // now lets blast that into the table by sheer man power!
  74. dtYourData.Load(rData);
  75. // close that reader!
  76. rData.Close();
  77. // close your connection to the spreadsheet!
  78. oCon.Close();
  79. // wow look at us go now! we are on a roll!!!!!
  80. // lets now see if our table has the spreadsheet data in it, shall we?
  81. Debug.Log(dtYourData.Rows.Count + " " + dtYourData.Rows.Count);
  82. if(dtYourData.Rows.Count > 0)
  83. {
  84. // do something with the data here
  85. // but how do I do this you ask??? good question!
  86. for (int i = 0; i < dtYourData.Rows.Count; i++)
  87. {
  88. // for giggles, lets see the column name then the data for that column!
  89. //Debug.Log(dtYourData.Columns[0].ColumnName + " : " + dtYourData.Rows[i][dtYourData.Columns[0].ColumnName].ToString());
  90. Debug.Log(dtYourData.Columns[0].ColumnName + " : " + dtYourData.Rows[i][dtYourData.Columns[0].ColumnName].ToString() + " | " + dtYourData.Columns[1].ColumnName + " : " + dtYourData.Rows[i][dtYourData.Columns[1].ColumnName].ToString() + " | " + dtYourData.Columns[2].ColumnName + " : " + dtYourData.Rows[i][dtYourData.Columns[2].ColumnName].ToString());
  91. }
  92. }
  93. }
  94. }