資源簡(jiǎn)介
unity讀取Excel
代碼片段和文件信息
using?UnityEngine;
using?System;
using?System.IO;
using?System.Data;
using?Excel;
using?OfficeOpenxml;
using?System.Collections.Generic;
using?ICSharpCode.SharpZipLib;
public?class?excel?:?MonoBehaviour
{
????//?Use?this?for?initialization
????void?Start()
????{
????????//WriteExcel(Application.dataPath?+?“/Book1.xls“);
????????//ReadExcel(Application.dataPath?+?“/Book1.xls“);
????}
????//?Update?is?called?once?per?frame
????void?Update()
????{
????}
????public?static?void?ReadExcel(string?outputDir)?
????{
????????FileStream?stream?=?File.Open(outputDir?FileMode.Open?FileAccess.Read);
????????IExcelDataReader?excelReader?=?ExcelReaderFactory.CreateOpenxmlReader(stream);
????????do
????????{
????????????//?sheet?name
????????????Debug.Log(excelReader.Name);
????????????while?(excelReader.Read())
????????????{
????????????????for?(int?i?=?0;?i?????????????????{
????????????????????string?value?=?excelReader.IsDBNull(i)???““?:?excelReader.GetString(i);
????????????????????Debug.Log(value);
????????????????}
????????????}
????????}?while?(excelReader.NextResult());
????}
????public?static?void?WriteExcel(string?outputDir)
????{
????????//string?outputDir?=?EditorUtility.SaveFilePanel(“Save?Excel“?““?“New?Resource“?“xlsx“);
????????FileInfo?newFile?=?new?FileInfo(outputDir);
????????if?(newFile.Exists)
????????{
????????????newFile.Delete();??//?ensures?we?create?a?new?workbook
????????????newFile?=?new?FileInfo(outputDir);
????????}
????????using?(ExcelPackage?package?=?new?ExcelPackage(newFile))
????????{
????????????//?add?a?new?worksheet?to?the?empty?workbook
????????????ExcelWorksheet?worksheet?=?package.Workbook.Worksheets.Add(“Sheet1“);
????????????//Add?the?headers
????????????worksheet.Cells[1?1].Value?=?“ID“;
????????????worksheet.Cells[1?2].Value?=?“Product“;
????????????worksheet.Cells[1?3].Value?=?“Quantity“;
????????????worksheet.Cells[1?4].Value?=?“Price“;
????????????worksheet.Cells[1?5].Value?=?“Value“;
????????????//Add?some?items...
????????????worksheet.Cells[“A2“].Value?=?12001;
????????????worksheet.Cells[“B2“].Value?=?“Nails“;
????????????worksheet.Cells[“C2“].Value?=?37;
????????????worksheet.Cells[“D2“].Value?=?3.99;
????????????worksheet.Cells[“A3“].Value?=?12002;
????????????worksheet.Cells[“B3“].Value?=?“Hammer“;
????????????worksheet.Cells[“C3“].Value?=?5;
????????????worksheet.Cells[“D3“].Value?=?12.10;
????????????worksheet.Cells[“A4“].Value?=?12003;
????????????worksheet.Cells[“B4“].Value?=?“Saw“;
????????????worksheet.Cells[“C4“].Value?=?12;
????????????worksheet.Cells[“D4“].Value?=?15.37;
????????????//save?our?new?workbook?and?we?are?done!
????????????package.Save();
????????}
????}
????
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????1068032??2016-01-08?10:00??Plugins\EPPlus.dll
?????文件????1584498??2016-01-08?10:00??Plugins\EPPlus.xm
?????文件??????68608??2014-01-30?20:10??Plugins\Excel.dll
?????文件?????237056??2014-01-30?20:10??Plugins\Excel.pdb
?????文件?????680960??2014-05-26?15:56??Plugins\I18N.CJK.dll
?????文件??????32256??2014-05-26?15:56??Plugins\I18N.dll
?????文件??????66048??2014-05-26?15:56??Plugins\I18N.West.dll
?????文件?????192512??2014-01-02?21:05??Plugins\ICSharpCode.SharpZipLib.dll
?????文件?????712192??2014-05-26?15:56??Plugins\System.Data.dll
?????文件???????2800??2017-06-06?12:40??excel.cs
?????目錄??????????0??2017-06-07?15:07??Plugins
-----------?---------??----------?-----??----
??????????????4644962????????????????????11
評(píng)論
共有 條評(píng)論