日期:2014-05-17  浏览次数:21061 次

C# 如何读取资源文件里面的Excel文件
C# 如何读取资源文件里面的Excel文件呢?图片就很好读取,但是Excel文件不知如何操作,因为Excel文件不能让用户看见里面的内容(如果加密的话又不能通过程序连接),所以想到把Excel文件作为资源文件嵌入到项目里面,我在程序里面会去读取这个文件并且返回一张表,应该如何读取呢?最好能够详细一点,搞定再加分。

下面这段代码会提示找不到Sheet1$:

  static ResourceManager rm =
 new ResourceManager("Resources.computers.xls", Assembly.GetExecutingAssembly());
   

  Setting mset = Setting.Instance;
  public static DataTable ReadXLS(string xlsDir)
  {  

  DataSet ds;
  string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" +
  "Extended Properties=Excel 8.0;" +
  "data source=" + xlsDir;
  OleDbConnection myConn = new OleDbConnection(strCon);
  string strCom = " SELECT * FROM [Sheet1$]";
  myConn.Open();
  OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
  ds = new DataSet();
  myCommand.Fill(ds);
  myConn.Close();
  return ds.Tables[0];
  }

  //DataTable dt=ComputerDLL.Computer_DLL.ReadXLS(Application.StartupPath + "\\File\\computers.xls");
   
  DataTable dt2 = ReadXLS(Application.StartupPath + "\\"+rm.BaseName);

------解决方案--------------------
请看这个

Sheet1$:

是EXcel的工作区
也许被用户生命名了

所以你要看清楚有没有这个名字的工作区
------解决方案--------------------
C# code
byte[] _ExcelFile = Properties.Resources.EXCEL资源名称;

  System.IO.FileStream _FileStream = new System.IO.FileStream(Application.StartupPath + "\\Temp.XLS", System.IO.FileMode.Create);
  _FileStream.Write(_ExcelFile, 0, _ExcelFile.Length);
  _FileStream.Close();

------解决方案--------------------
[assembly: WebResource("Revenco.Web.DevelopKit.Scripts.Charts.Area2D.swf", "application/x-shockwave-flash")]

上面先声明资源,下面获取。

 return page.ClientScript.GetWebResourceUrl(type, string.Format("Revenco.Web.DevelopKit.Scripts.Charts.{0}", swfName));