Hi, I’m trying to use excel in my C# code.
I need excel to read multiple datasheets and assigned each of them to different table.
I’ve succeeded in loading them, but then realize that the new one that got read override previous ones.
Can anyone help me with this?
public DataTable Data_ShelfData;
public DataTable Data_BookData;
public DataTable Data_ControlData;
void Awake()
{
string fileLocation = Application.dataPath + "/Library/DataBase/BookDatabase.xls";
Data_ShelfData = readXLS(fileLocation, "ShelfData");
Data_BookData = readXLS(fileLocation, "BookData");
Data_ControlData = readXLS(fileLocation, "Control");
}
void Start ()
{
Debug.Log(getData(Data_ShelfData, 0, 1));
Debug.Log(getData(Data_BookData, 0, 1));
Debug.Log(getData(Data_ControlData, 0, 1));
}
DataTable readXLS( string FileToRead, string SheetToRead)
{
// Must be saved as excel 2003 workbook, not 2007, mono issue really
string con = "Driver={Microsoft Excel Driver (*.xls)}; DriverId=790; Dbq="+FileToRead+";";
//Debug.Log(con);
string yourQuery = "SELECT * FROM [" + SheetToRead + "$]";
OdbcConnection oCon = new OdbcConnection(con);
OdbcCommand oCmd = new OdbcCommand(yourQuery, oCon);
DataTable TableToSave = new DataTable(SheetToRead);
oCon.Open();
OdbcDataReader rData = oCmd.ExecuteReader();
TableToSave.Load(rData);
rData.Close();
oCon.Close();
return TableToSave;
}
string getData(DataTable Table, int row, int column)
{
//format Table.[row][column]
return Data_ControlData.Rows[row][Data_ControlData.Columns[column]].ToString();
}