- Home /
multiple excel sheet not working preoperly
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();
}
Comment
Best Answer
Answer by Levantez · May 21, 2012 at 04:10 PM
Figured it out. Database managed to read successfully, but the getData log code was wrong.
string getData(DataTable Table, int row, int column)
{
//format Table.[row][column]
return DataTable.Rows[row][DataTable.Columns[column]].ToString();
}
Answer by rosschanson · May 27, 2013 at 12:25 AM
You may fix excel sheet after reading article excel fix
Your answer
Follow this Question
Related Questions
Good way to read/write .xlsx files 2 Answers
How to import data into Microsoft Excel? 1 Answer
Best tool to load data 1 Answer
Read XLS file with UTF-8 character; 1 Answer
Keeping object data in an excel sheet? 2 Answers