解析Excel 2007 文件 - 取得文字內容

解析Excel 2007 文件 - 取得文字內容

加入WindowsBase.dll 到參考

using System.IO;
using System.IO.Compression;
using System.IO.Packaging;
using System.Xml;
...
private void button1_Click(object sender, EventArgs e)
{            
      OpenFileDialog op = new OpenFileDialog();
      op.DefaultExt = "xlsx";
      op.Filter = "Excel 2007 文件|*.xlsx";
      op.Title = "開啟Excel 2007 文件";
      if (op.ShowDialog() == DialogResult.OK)
      {
        using (Package package = Package.Open(op.FileName, FileMode.Open, FileAccess.Read))
        {
              Uri xlsx_Uri = new Uri("/xl/sharedStrings.xml", UriKind.Relative);
              PackagePart xlsx_Part = package.GetPart(xlsx_Uri);
              XmlDocument xlsx_XmlDocument = new XmlDocument();
              xlsx_XmlDocument.Load(xlsx_Part.GetStream());
              MessageBox.Show(xlsx_XmlDocument.InnerText, "message");
              package.Close();
          }
      }
}