• 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

  • 相关阅读:
    关于串口
    assigning to uiimagepickercontrollerdelegate from incompatible type
    CodeSign error: code signing is required for product type 'Application' in SDK 'iOS 7.0'
    使用和不使用navigationbar分别处理显示和返回页面
    iphone坐标系统
    A valid provisioning profile for this executable was not found.
    Could not change executable permissions on the application
    Code Sign error: a valid provisioning profile matching the application's Identifier 'com. sensoSource.VoiceRecorder' could not be found
    从零开始开发iPhone,教你如何在真机调试iPhone应用程序
    iPhone 证书导出分享给多个开发人员操作
  • 原文地址:https://www.cnblogs.com/salam/p/1805367.html
Copyright © 2020-2023  润新知