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

  • 相关阅读:
    BZOJ3670:[NOI2014]动物园(KMP)
    415. [HAOI2009] 旅行
    U10223 Cx大帝远征埃及
    U10206 Cx的治疗
    2741. [济南集训 2017] 掰巧克力
    复习题目汇总 over
    7-20 表达式转换(25 分)
    7-19 求链式线性表的倒数第K项(20 分)(单链表定义与尾插法)
    7-18 银行业务队列简单模拟(25 分)
    7-17 汉诺塔的非递归实现(25 分)(有待改进)
  • 原文地址:https://www.cnblogs.com/salam/p/1805367.html
Copyright © 2011-2022 走看看