读取xml并将节点保存到Excal

using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;

namespace myXMLReader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            treeResult.Nodes.Clear();
            readXml();
        }

        private void readXml()
        {
            // todo
            String sourcePath = "f:/test.xml";

            System.Xml.XmlDocument sourceXml = new XmlDocument();
            try
            {
                sourceXml.Load(sourcePath);
            }
            catch (XmlException e)
            {
                StringBuilder sb = addRootToXml(sourcePath);
                sourceXml.LoadXml(sb.ToString());
            }
            catch (Exception e)
            {
                return;
            }

            foreach (XmlNode rootNode in sourceXml.ChildNodes)
            {
                if (rootNode.NodeType == XmlNodeType.Element)
                {
                    TreeNode tNode = new TreeNode(rootNode.Name);
                    readChildNode(rootNode, tNode);
                    treeResult.Nodes.Add(tNode);
                }
            }


            creatToExcel();
        }

        private void readChildNode(XmlNode node, TreeNode tNode)
        {
            foreach (XmlNode childNode in node.ChildNodes)
            {
                if (childNode.NodeType != XmlNodeType.Element)
                {
                    continue;
                }
                TreeNode tChildNode = new TreeNode(childNode.Name);
                tNode.Nodes.Add(tChildNode);
                if (childNode.HasChildNodes)
                {
                    readChildNode(childNode, tChildNode);
                }
            }
        }
        private StringBuilder addRootToXml(string path)
        {
            TextReader reader = File.OpenText(path);
            StringBuilder sb = new StringBuilder(reader.ReadToEnd());
            sb.Insert(0, "<XML>");
            sb.Append("</XML>");
            return sb;
        }

        private int writeRowIndex = 1;
        private void creatToExcel()
        {
            int startColumn = 1;

            HSSFWorkbook hssfworkbook = new HSSFWorkbook();

            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "NPOI Team";
            hssfworkbook.DocumentSummaryInformation = dsi;

            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "NPOI SDK Example";
            hssfworkbook.SummaryInformation = si;

            ISheet sheet = hssfworkbook.CreateSheet("Sheet1");

            writeNodeToExcel(sheet, treeResult.Nodes[0], startColumn);

            FileStream file = new FileStream(@"f:/test.xls", FileMode.Create);
            hssfworkbook.Write(file);
            file.Close();

        }

        private void writeNodeToExcel(ISheet sheet, TreeNode node, int columnIndex)
        {
            IRow row = sheet.CreateRow(getRow());
            ICell cell = row.CreateCell(columnIndex);
            cell.SetCellValue(node.Text);
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                writeNodeToExcel(sheet, node.Nodes[i], columnIndex + 1);
            }
        }

        private int getRow()
        {
            return writeRowIndex++;
        }
    }
}

最近需要一个读取xml,将节点写入Excel的功能,还没有完善,暂时记录一下。

转载于:https://www.cnblogs.com/qiangwei/p/4297730.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值