代码改变世界

Excel2007数据导入到Silverlight DataGrid中

2011-10-26 10:12  elivsit  阅读(273)  评论(0编辑  收藏  举报

一。读取Excel(xlsx)文件数据

  xlsx文件是由一个压缩文件和一个载有关于什么是内部的拉链系列信息的XML文件.

 public class UnZipper
    {
        private Stream stream;

        public UnZipper(Stream zipFileStream)
        {
            this.stream = zipFileStream;
        }

        /// <summary>
        /// 获取文件流
        /// </summary>
        /// <param name="filename">文件的全路径</param>
        /// <returns></returns>
        public Stream GetFileStream(string filename)
        {
            //文件地址
            Uri fileUri = new Uri(filename, UriKind.Relative);
            //保存
            StreamResourceInfo info = new StreamResourceInfo(this.stream, null);
            if (this.stream is System.IO.FileStream)
            {
                this.stream.Seek(0, SeekOrigin.Begin);
            }

            StreamResourceInfo stream = System.Windows.Application.GetResourceStream(info, fileUri);
            if (stream != null)
            {
                return stream.Stream;
            }
            return null;
        }

        public IEnumerable<string> GetFileNamesInZip()
        {
            BinaryReader reader = new BinaryReader(stream);
            stream.Seek(0, SeekOrigin.Begin);
            string name = null;
            List<string> names = new List<string>();
            while (ParseFileHeader(reader, out name))
            {
                names.Add(name);
            }
            return names;
        }

        /// <summary>
        /// 读取内容
        /// </summary>
        /// <param name="reader">流</param>
        /// <param name="filename">文件名</param>
        /// <returns></returns>
        private static bool ParseFileHeader(BinaryReader reader, out string filename)
        {
            filename = null;
            if (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                int headerSignature = reader.ReadInt32();
                if (headerSignature == 67324752) //ggggggrrrrrrrrrrrrrrrrr
                {
                    reader.BaseStream.Seek(2, SeekOrigin.Current);

                    short genPurposeFlag = reader.ReadInt16();
                    if (((((int)genPurposeFlag) & 0x08) != 0))
                        return false;
                    reader.BaseStream.Seek(10, SeekOrigin.Current);

                    int compressedSize = reader.ReadInt32();
                    int unCompressedSize = reader.ReadInt32();
                    short fileNameLenght = reader.ReadInt16();
                    short extraFieldLenght = reader.ReadInt16();
                    filename = new string(reader.ReadChars(fileNameLenght));
                    if (string.IsNullOrEmpty(filename))
                        return false;

                    reader.BaseStream.Seek(extraFieldLenght + compressedSize, SeekOrigin.Current);
                    if (unCompressedSize == 0)
                        return ParseFileHeader(reader, out filename);
                    else
                        return true;
                }
            }
            return false;
        }

}
   

 

转载自:http://www.silverlightshow.net/items/An-Excel-file-Viewer-in-Silverlight-4.aspx

源代码下载地址:http://www.snello.it/SharedFiles/Download.aspx?pageid=3&fileid=7&mid=12