zoukankan      html  css  js  c++  java
  • Excel2007数据导入到Silverlight DataGrid中(转载)

    一。读取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

  • 相关阅读:
    linux共享库的版本控制
    c++:自己动手实现线程安全的c++单例类
    c++设计成员变量可动态调整的动态类结构
    c++中std::set自定义去重和排序函数
    python网络编程调用recv函数完整接收数据的三种方法
    c++ 以二进制和以文本方式读写文件的区别
    c++和python如何实现主机字节序和网络字节序的相互转换
    线控性能比拼,MKZ与CRV作为自动驾驶开发平台的全面测评
    css 简易 loading 加载图
    Element UI 中被隐藏的滚动条
  • 原文地址:https://www.cnblogs.com/salam/p/1805367.html
Copyright © 2011-2022 走看看