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

  • 相关阅读:
    字符序列(characts)
    装载问题(load)
    哈密顿路
    犯罪团伙
    回溯算法
    维修机器人
    旅行计划
    皇后游戏
    运输
    亲身实测可用的java实现wordxlsxpdf文件下载功能
  • 原文地址:https://www.cnblogs.com/salam/p/1805367.html
Copyright © 2011-2022 走看看