打包Epplus 设置
阿斯蒂阿斯蒂
#代码 1级标题
using System;
using System.Data;
using System.IO;
using System.Linq;
using OfficeOpenXml;
using UnityEngine;
public class TestManniu : MonoSingleton
{
///
/// Excel文件名
///
private string excelName = “Train”;
/// <summary>
/// Excel文件Sheet名称
/// </summary>
private string sheetName = "Sheet1";
public string ExcelPath
{
get
{
return Application.streamingAssetsPath + "/" + excelName + ".xls";
}
}
private void Start()
{
// DataTable dataTable = ReadExcelToDataTable(ExcelPath, sheetName, true);
// Debug.Log(dataTable.Rows.Count);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
WriteExcel("张三", "2323");
}
}
/// <summary>
/// 写入数据到Excel
/// </summary>
/// <param name="id">训练者唯一ID</param>
/// <param name="trainerName">训练者姓名</param>
public void WriteExcel(string id, string trainerName)
{
FileInfo fileInfo = new FileInfo(ExcelPath);
if (!fileInfo.Exists)
{
using (ExcelPackage package = new ExcelPackage(fileInfo))
{
// 在 excel 空文件添加新 sheet,并设置名称。
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(sheetName);
worksheet.Cells[1, 1].Value = "ID";
worksheet.Cells[1, 2].Value = "Name";
worksheet.Cells[1, 3].Value = "StartTime";
worksheet.Cells[1, 4].Value = "EndTime";
worksheet.Cells[2, 1].Value = id;
worksheet.Cells[2, 2].Value = trainerName;
worksheet.Cells[2, 3].Value = DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
worksheet.Cells[2, 4].Value = "20";
package.Save();
}
}
else // 在原Excel基础上进行写入数据
{
int index = ReadExcelToDataTable(ExcelPath, sheetName, true).Rows.Count + 2;
using (ExcelPackage package = new ExcelPackage(fileInfo))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault(go => go.Name == sheetName);
worksheet.Cells[index, 1].Value = id;
worksheet.Cells[index, 2].Value = trainerName;
worksheet.Cells[index, 3].Value = DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
worksheet.Cells[index, 4].Value = "训练结束时填写";
package.Save();
}
}
}
/// 将excel中的数据导入到DataTable中
///
/// excel文件的名称
/// excel工作薄sheet的名称
/// 第一行是否是属性
/// 返回的DataTable
public DataTable ReadExcelToDataTable(string fileName, string sheetName, bool isFirstRowColumn)
{
if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(sheetName)) return null;
try
{
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var package = new ExcelPackage(fs);
DataTable data = new DataTable();
ExcelWorkbook workBook = package.Workbook;
if (workBook != null)
{
if (workBook.Worksheets.Count > 0)
{
ExcelWorksheet currentWorksheet = workBook.Worksheets[sheetName];
int lastRow = currentWorksheet.Dimension.End.Row;
int lastColumn = currentWorksheet.Dimension.End.Column;
int columnCount = 1;
while (columnCount <= lastColumn)
{
data.Columns.Add(Convert.ToString(currentWorksheet.Cells[1, columnCount].Value));
columnCount++;
}
int rowCount = 0;
if (isFirstRowColumn) rowCount = currentWorksheet.Dimension.Start.Row + 1;
else rowCount = currentWorksheet.Dimension.Start.Row;
while (rowCount <= lastRow)
{
columnCount = 1;
DataRow newRow = data.NewRow();
while (columnCount <= lastColumn)
{
newRow[data.Columns[columnCount - 1]] =
Convert.ToString(currentWorksheet.Cells[rowCount, columnCount].Value);
Debug.Log(currentWorksheet.Cells[rowCount, columnCount].Value);
columnCount++;
}
rowCount++;
data.Rows.Add(newRow);
}
}
fs.Close();
fs.Dispose();
}
return data;
}
}
catch (Exception ex)
{
//LogHelper.WriteLog(ex);
return null;
}
}
}