- Home /
read an excel file works only in the editor,not in the build
hello, here's my code for read from excel file. editor works fine but in the build the excel file is not read.
when i work in the editor the file is in the folder
c:\projectname\1.xls
in the build:
c:\1.xls
c:\project_Data\ 1.xls
using UnityEngine;
using System.Collections;
using System;
using System.Data;
using System.Data.Odbc;
public class ExcelReader : MonoBehaviour
{
String something;
public String Filename="1";
void Start ()
{
readXLS(Filename);
}
void readXLS(String Filename)
{
// Must be saved as excel 2003 workbook, not 2007, mono issue really
string con = "Driver={Microsoft Excel Driver (*.xls)}; DriverId=790; Dbq="+Filename+";";
string yourQuery = "SELECT * FROM [Foglio1$]";
OdbcConnection oCon = new OdbcConnection(con);
OdbcCommand oCmd = new OdbcCommand(yourQuery, oCon);
DataTable dtYourData = new DataTable("YourData");
oCon.Open();
OdbcDataReader rData = oCmd.ExecuteReader();
dtYourData.Load(rData);
rData.Close();
oCon.Close();
if(dtYourData.Rows.Count > 0)
{
prova = dtYourData.Rows[0][dtYourData.Columns[1].ColumnName].ToString();
if (something == "DESTROY!")
Destroy(GameObject.Find("Cube"));
print(something);
}
}
}
What is "prova" and where is "something" set? OR should "prova" be "something"?
Why do you want to read xls? Use a more general format like xml or csv if you want to work with excel. xml or csv can easily be read on all platforms. The odbc driver for excel does only work for windows builds.
Answer by vickartinson39 · Jun 16, 2013 at 07:42 PM
Read excel files by the use of excel repair file
Apply http://www.excel2007repair.excelrepairtoolbox.com contains a lot of effective solutions
Answer by Owen-Reynolds · Aug 02, 2012 at 04:09 PM
The build tries to keep small by stripping out anything it doesn't know you're using. really nice feature. It checks for anything on a gameObject, Material ... and anything which has been dragged into a public
variable in an Inspector. But, it can't "see" things which are only accessed in code.
So, it's probably purposely not including your file.
The Unity work-around for that is to put anything like that into a folder named Resources. Anything in there is always included in the build. Not sure if caps matter, or it needs to be a single top-level Resources folder. But you should be able to just examine the new build-folder and see if it's there.
Code stripping is a mobile only feature afaik. On all other platforms everything is included, even classes or scripts that aren't used anywhere.
@leonida: $$anonymous$$y first question is, what target platform do you try to build for?
Your answer
![](https://koobas.hobune.stream/wayback/20220613075056im_/https://answers.unity.com/themes/thub/images/avi.jpg)