在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例常用C#方法 → NPOI 不安装office 创建和读取office文件 包括 excel和word文件 项目完整源码下载

NPOI 不安装office 创建和读取office文件 包括 excel和word文件 项目完整源码下载

常用C#方法

下载此实例
  • 开发语言:C#
  • 实例大小:2.07M
  • 下载次数:115
  • 浏览次数:3628
  • 发布时间:2013-09-29
  • 实例类别:常用C#方法
  • 发 布 人:crazycode
  • 文件格式:.zip
  • 所需积分:2
 相关标签: Word Excel 文件

实例介绍

【实例简介】
【实例截图】

【核心代码】

/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for Additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

namespace NPOI
{
    using System;
    using System.IO;
    using System.Collections;
    using NPOI.Util;
    using NPOI.POIFS.FileSystem;
    using NPOI.HPSF;
    using System.Collections.Generic;


    /// <summary>
    /// This holds the common functionality for all POI
    /// Document classes.
    /// Currently, this relates to Document Information Properties
    /// </summary>
    /// <remarks>@author Nick Burch</remarks>
    [Serializable]
    public abstract class POIDocument
    {
        /** Holds metadata on our document */
        protected SummaryInformation sInf;
        /** Holds further metadata on our document */
        protected DocumentSummaryInformation dsInf;
        /**	The directory that our document lives in */
        protected DirectoryNode directory;

        /** For our own logging use */
        //protected POILogger logger;

        /* Have the property streams been Read yet? (Only done on-demand) */
        protected bool initialized = false;

        protected POIDocument(DirectoryNode dir)
        {
            this.directory = dir;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="POIDocument"/> class.
        /// </summary>
        /// <param name="dir">The dir.</param>
        /// <param name="fs">The fs.</param>
        [Obsolete]
        public POIDocument(DirectoryNode dir, POIFSFileSystem fs)
        {
            this.directory = dir;
            //POILogFactory.GetLogger(this.GetType());
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="POIDocument"/> class.
        /// </summary>
        /// <param name="fs">The fs.</param>
        public POIDocument(POIFSFileSystem fs)
            : this(fs.Root) 
        {
            
        }
        /**
	 * Will create whichever of SummaryInformation
	 *  and DocumentSummaryInformation (HPSF) properties
	 *  are not already part of your document.
	 * This is normally useful when creating a new
	 *  document from scratch.
	 * If the information properties are already there,
	 *  then nothing will happen.
	 */
        public void CreateInformationProperties()
        {
            if (!initialized) ReadProperties();
            if (sInf == null)
            {
                sInf = PropertySetFactory.CreateSummaryInformation();
            }
            if (dsInf == null)
            {
                dsInf = PropertySetFactory.CreateDocumentSummaryInformation();
            }
        }
        // nothing to dispose
        //public virtual void Dispose()
        //{
        //
        //}
        /// <summary>
        /// Fetch the Document Summary Information of the document
        /// </summary>
        /// <value>The document summary information.</value>
        public DocumentSummaryInformation DocumentSummaryInformation
        {
            get
            {
                if (!initialized) ReadProperties();
                return dsInf;
            }
            set 
            {
                dsInf = value;
            }
        }

        /// <summary>
        /// Fetch the Summary Information of the document
        /// </summary>
        /// <value>The summary information.</value>
        public SummaryInformation SummaryInformation
        {
            get
            {
                if (!initialized) ReadProperties();
                return sInf;
            }
            set 
            {
                sInf = value;
            }
        }

        /// <summary>
        /// Find, and Create objects for, the standard
        /// Documment Information Properties (HPSF).
        /// If a given property Set is missing or corrupt,
        /// it will remain null;
        /// </summary>
        protected void ReadProperties()
        {
            PropertySet ps;

            // DocumentSummaryInformation
            ps = GetPropertySet(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            if (ps != null && ps is DocumentSummaryInformation)
            {
                dsInf = (DocumentSummaryInformation)ps;
            }
            else if (ps != null)
            {
                //logger.Log(POILogger.WARN, "DocumentSummaryInformation property Set came back with wrong class - ", ps.GetType());
            }

            // SummaryInformation
            ps = GetPropertySet(SummaryInformation.DEFAULT_STREAM_NAME);
            if (ps is SummaryInformation)
            {
                sInf = (SummaryInformation)ps;
            }
            else if (ps != null)
            {
                //logger.Log(POILogger.WARN, "SummaryInformation property Set came back with wrong class - ", ps.GetType());
            }

            // Mark the fact that we've now loaded up the properties
            initialized = true;
        }

        /// <summary>
        /// For a given named property entry, either return it or null if
        /// if it wasn't found
        /// </summary>
        /// <param name="SetName">Name of the set.</param>
        /// <returns></returns>
        protected PropertySet GetPropertySet(String SetName)
        {
            //directory can be null when creating new documents
            if (directory == null) return null;
            DocumentInputStream dis;
            try
            {
                // Find the entry, and Get an input stream for it
                dis = directory.CreateDocumentInputStream(SetName);
            }
            catch (IOException)
            {
                // Oh well, doesn't exist
                //logger.Log(POILogger.WARN, "Error Getting property Set with name "   SetName   "\n"   ie);
                return null;
            }

            try
            {
                // Create the Property Set
                PropertySet Set = PropertySetFactory.Create(dis);
                return Set;
            }
            catch (IOException)
            {
                // Must be corrupt or something like that
                //logger.Log(POILogger.WARN, "Error creating property Set with name "   SetName   "\n"   ie);
            }
            catch (HPSFException)
            {
                // Oh well, doesn't exist
                //logger.Log(POILogger.WARN, "Error creating property Set with name "   SetName   "\n"   he);
            }
            return null;
        }

        /// <summary>
        /// Writes out the standard Documment Information Properties (HPSF)
        /// </summary>
        /// <param name="outFS">the POIFSFileSystem to Write the properties into</param>
        protected void WriteProperties(POIFSFileSystem outFS)
        {
            WriteProperties(outFS, null);
        }
        /// <summary>
        /// Writes out the standard Documment Information Properties (HPSF)
        /// </summary>
        /// <param name="outFS">the POIFSFileSystem to Write the properties into.</param>
        /// <param name="writtenEntries">a list of POIFS entries to Add the property names too.</param>
        protected void WriteProperties(POIFSFileSystem outFS, IList writtenEntries)
        {
            if (sInf != null)
            {
                WritePropertySet(SummaryInformation.DEFAULT_STREAM_NAME, sInf, outFS);
                if (writtenEntries != null)
                {
                    writtenEntries.Add(SummaryInformation.DEFAULT_STREAM_NAME);
                }
            }
            if (dsInf != null)
            {
                WritePropertySet(DocumentSummaryInformation.DEFAULT_STREAM_NAME, dsInf, outFS);
                if (writtenEntries != null)
                {
                    writtenEntries.Add(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
                }
            }
        }

        /// <summary>
        /// Writes out a given ProperySet
        /// </summary>
        /// <param name="name">the (POIFS Level) name of the property to Write.</param>
        /// <param name="Set">the PropertySet to Write out.</param>
        /// <param name="outFS">the POIFSFileSystem to Write the property into.</param>
        protected void WritePropertySet(String name, PropertySet Set, POIFSFileSystem outFS)
        {
            try
            {
                MutablePropertySet mSet = new MutablePropertySet(Set);
                using (MemoryStream bOut = new MemoryStream())
                {
                    mSet.Write(bOut);
                    byte[] data = bOut.ToArray();
                    using (MemoryStream bIn = new MemoryStream(data))
                    {
                        outFS.CreateDocument(bIn, name);
                    }
                    //logger.Log(POILogger.INFO, "Wrote property Set "   name   " of size "   data.Length);
                }
            }
            catch (WritingNotSupportedException)
            {
                Console.Error.WriteLine("Couldn't Write property Set with name "   name   " as not supported by HPSF yet");
            }
        }

        /// <summary>
        /// Writes the document out to the specified output stream
        /// </summary>
        /// <param name="out1">The out1.</param>
        public abstract void Write(Stream out1);

        /// <summary>
        /// Copies nodes from one POIFS to the other minus the excepts
        /// </summary>
        /// <param name="source">the source POIFS to copy from.</param>
        /// <param name="target">the target POIFS to copy to</param>
        /// <param name="excepts">a list of Strings specifying what nodes NOT to copy</param>
        [Obsolete]
        protected void CopyNodes(POIFSFileSystem source, POIFSFileSystem target,
                                  List<String> excepts)
        {
            POIUtils.CopyNodes(source, target, excepts);
        }
        /// <summary>
        /// Copies nodes from one POIFS to the other minus the excepts
        /// </summary>
        /// <param name="sourceRoot">the source POIFS to copy from.</param>
        /// <param name="targetRoot">the target POIFS to copy to</param>
        /// <param name="excepts">a list of Strings specifying what nodes NOT to copy</param>
        [Obsolete]
        protected void CopyNodes(DirectoryNode sourceRoot,
                DirectoryNode targetRoot, List<String> excepts)
        {
            POIUtils.CopyNodes(sourceRoot, targetRoot, excepts);
        }

        /// <summary>
        /// Checks to see if the String is in the list, used when copying
        /// nodes between one POIFS and another
        /// </summary>
        /// <param name="entry">The entry.</param>
        /// <param name="list">The list.</param>
        /// <returns>
        /// 	<c>true</c> if [is in list] [the specified entry]; otherwise, <c>false</c>.
        /// </returns>
        private bool isInList(String entry, IList list)
        {
            for (int k = 0; k < list.Count; k  )
            {
                if (list[k].Equals(entry))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Copies an Entry into a target POIFS directory, recursively
        /// </summary>
        /// <param name="entry">The entry.</param>
        /// <param name="target">The target.</param>
        [Obsolete]
        private void CopyNodeRecursively(Entry entry, DirectoryEntry target)
        {
            //System.err.println("copyNodeRecursively called with " entry.Name 
            //                   "," target.Name);
            POIUtils.CopyNodeRecursively(entry, target);
        }
    }
}

标签: Word Excel 文件

实例下载地址

NPOI 不安装office 创建和读取office文件 包括 excel和word文件 项目完整源码下载

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 qq53236828 发表于: 2014-08-04 10:02 20
貌似下载不了

支持(0) 盖楼(回复)

第 2 楼 fengyun871210 发表于: 2015-01-13 11:44 50
我觉得门槛有点高,什么都没看到,就需要先上传自己的东西或者充值

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有2条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警