温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Unity读取Excel文件转换XML格式文件的方法

发布时间:2020-06-23 09:34:33 来源:亿速云 阅读:204 作者:清晨 栏目:开发技术

不懂Unity读取Excel文件转换XML格式文件的方法?其实想解决这个问题也不难,下面让小编带着大家一起学习怎么去解决,希望大家阅读完这篇文章后大所收获。

此方法用到excel.dll

下载连接 点击打开链接

using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml;
using Excel;
using System.Data;
 
/// <summary>
/// 创建XML表
/// </summary>
public class CreateXML : MonoBehaviour
{
 /// <summary>
 /// 表头
 /// </summary>
 public const string xmlRoot = "FZW_MASK_XML_TABLE";
 
 //Excel名字
 public string ExcelPathName;
 
 //xml文件路径;
 private string Path;
 //表文件名
 public string xmlName = "XMLTABLE.xml";
 //表名
 public string xmlTabeName = "XMLTABLE";
 
 //第一行字段
 private string[] tableTop;
 
 //表List
 private List<string[]> tableList=new List<string[]>();
 
 
 private void Awake()
 {
  //设置路径
  Path = Application.streamingAssetsPath + "/XMLTable/" + xmlName;
 
  //读取Excel
  ReadExcel(ExcelPathName);
 }
 
 
 /// <summary>
 /// 读Excel
 /// </summary>
 /// <param name="ExcelPath"></param>
 /// <returns></returns>
 public void ReadExcel(string ExcelPath)
 {
  //excel文件位置 /MaskGame/ReadExcel/excel文件名
  FileStream stream = File.Open(Application.dataPath + "/MaskGame/ReadExcel/" + ExcelPath, FileMode.Open, FileAccess.Read);
  IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
 
  DataSet result = excelReader.AsDataSet();
 
  int rows = result.Tables[0].Rows.Count;//获取行数(多少行信息)
  int columns = result.Tables[0].Columns.Count;//获取列数(多少列字段)
  
  
  //初始化字段
  tableTop = new string[columns];
  
 
  //存字段
  for (int i = 0; i < columns; i++)
  {
   tableTop[i]= result.Tables[0].Rows[0][i].ToString();
  }
 
  //从第二行开始读 读信息
  for (int i = 1; i < rows; i++)
  {
   //临时表
   string[] table = new string[columns];
   //赋值表信息
   for (int j = 0; j < columns; j++)
   {
    string nvalue = result.Tables[0].Rows[i][j].ToString();
    table[j] = nvalue; 
   }
   //添加到List
   tableList.Add(table);
  }
 }
 
 /// <summary>
 /// 创建表格
 /// </summary>
 private void CreateXMLTable()
 {
  //路径错误
  if (File.Exists(Path)) return;
 
  //xml对象;
  XmlDocument xmll = new XmlDocument();
  //跟节点
  XmlElement Root = xmll.CreateElement(xmlRoot);
 
  for (int i = 0; i < tableList.Count; i++)
  {
   XmlElement xmlElement = xmll.CreateElement(xmlTabeName);
   xmlElement.SetAttribute(tableTop[0], tableList[i][0]);
 
   for (int j = 0; j < tableTop.Length-1; j++)
   {
    XmlElement infoElement = xmll.CreateElement(tableTop[j + 1]);
    infoElement.InnerText = tableList[i][j + 1];
    xmlElement.AppendChild(infoElement);
   }
   Root.AppendChild(xmlElement);
  }
 
  xmll.AppendChild(Root);
  xmll.Save(Path);
 
 }
 
 void OnGUI()
 {
  if (GUI.Button(new Rect(200, 200, 500, 500), "创建XML表"))
  {
   CreateXMLTable();
   Debug.Log("创建成功: " + Path);
  }
  
 }
}

感谢你能够认真阅读完这篇文章,希望小编分享Unity读取Excel文件转换XML格式文件的方法内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI