Unity 使用NPOI,模板替換Excel中的關(guān)鍵字(針對.xlsx)
需求:項目中要用到生成Excel來打印文件,只需要替換其中的值,保留原模板,生成新的Excel
第一步:在unity中導(dǎo)入一下的dll
新建一個Plugin的文件夾,把dll全部放進去
以上選中的這些文件在unity的安裝目錄下Unity\Editor\Data\Mono\lib\mono\unity可以找到
還有一個System.Data.dll,我放進去它會顯示重復(fù)引用,所以我就沒放上去,你要是想試試也可以在安裝路徑下找到,然后放進去
其他的鏈接在這里下載:Dll下載地址
第二步:新建一個Excel,取名為量表.xlsx(這個自己定,后面的代碼記得改,但是后綴一定時.xlsx)
放在StreamingAssets下面,作為Excel的模板路徑
例如,現(xiàn)在的這個數(shù)值是和后面代碼相掛鉤的
第三步:新建腳本,隨便掛載在物體上
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class Writing : MonoBehaviour
{
/// <summary>
/// 模板文件路徑
/// </summary>
private string filePath = Application.streamingAssetsPath;
///目標(biāo)路徑,修改后的文件路徑
private string targetPath = Application.streamingAssetsPath + "/Print/";
/// <summary>
/// 文件名稱
/// </summary>
private string fileName = "量表.xlsx";
private string path = "";
Dictionary<string, string> userInfo = new Dictionary<string, string>();
void Start()
{
SetFile();
//key為要替換的關(guān)鍵詞,value為將要替換的值
userInfo.Add("ABC", "是我");
userInfo.Add("ABC1", "是我1");
userInfo.Add("ABC2", "是我2");
userInfo.Add("ABC3", "是我3");
WriteExcelTest();
}
public void SetFile()
{
//獲取指定路徑下面的所有資源文件 ,每次都刪除這個路徑下的文件
if (Directory.Exists(Application.streamingAssetsPath + "/Print/"))
{
DirectoryInfo direction = new DirectoryInfo(Application.streamingAssetsPath + "/Print/");
FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; ++i)
{
if (files[i].Name.Contains(".xlsx"))
{
File.Delete(Application.streamingAssetsPath + "/Print/" + files[i].Name);//刪除這個目錄下所有的word文檔
}
}
}
else
{
Directory.CreateDirectory(Application.streamingAssetsPath + "/Print/");
print("創(chuàng)建成功");
}
}
public void WriteExcelTest()
{
//合并兩個路徑字符串,整合模板文件的路徑
path = Path.Combine(filePath, fileName);
//打開模板文件
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
//新建一個Excel
IWorkbook workbook = new XSSFWorkbook(fs);
//根據(jù)索引獲取第一個表
ISheet sheet = workbook.GetSheetAt(0);
for (int i = sheet.FirstRowNum; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
for (int j = row.FirstCellNum; j < row.LastCellNum; j++)
{
ICell cell = row.GetCell(j);
//得到原來的值
string cellContent = cell.StringCellValue;
//替換和字典對應(yīng)的值
foreach (KeyValuePair<string, string> item in userInfo)
{
print(cellContent + " --------------");
if (item.Key == cellContent)
{
cell.SetCellValue(item.Value);
}
}
}
}
targetPath = targetPath + fileName;
FileStream output = new FileStream(targetPath, FileMode.Create);//生成指定文件,這里把路徑改為目標(biāo)路徑
workbook.Write(output);//寫入文件
//一些列關(guān)閉釋放操作
fs.Close();
fs.Dispose();
output.Close();
output.Dispose();
Debug.Log("修改文件成功");
System.Diagnostics.Process.Start(targetPath);//打開文檔
}
}
運行結(jié)果為
新生成的文件路徑為
大功告成,后續(xù)再增加其他的Excel內(nèi)容
補充:當(dāng)遍歷整個excel的時候,如果有為1,2,3這樣數(shù)值填充的單元格,會報錯誤InvalidOperationException: Cannot get a text value from a numeric cell NPOI.XSSF.UserModel.XSSFCell.get_RichStringCellValue ()文章來源:http://www.zghlxwxcb.cn/news/detail-423694.html
解決辦法:在遍歷的時候新添加幾行代碼
if (row.GetCell(j) != null)
{
row.GetCell(j).SetCellType(CellType.String);
}文章來源地址http://www.zghlxwxcb.cn/news/detail-423694.html
到了這里,關(guān)于Unity 使用NPOI,模板替換Excel中的關(guān)鍵字(針對.xlsx)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!