EPPlus 是使用Open Office XML格式(xlsx)讀寫Excel 2007 / 2010文件的.net開發(fā)庫。
官網(wǎng):http://epplus.codeplex.com/
基于EEPlus在Unity編輯器中導出Excel文件。
Step1:
到官網(wǎng) http://epplus.codeplex.com/ 下載eeplus.dll文件拖入Unity 工程中。
Step2:
Create EPPlusExporter.cs 并輸入下面代碼
[MenuItem("EPPlusTool/ExportXlxsFile")]
public static void CreateSheet(){
string exportPath = EditorUtility.SaveFilePanel ("Save SkuInfo File", "", "prefabInfo.xlsx", "xlsx");
if (File.Exists (exportPath)) {
File.Delete (exportPath);
}
using (ExcelPackage package=new ExcelPackage(new FileInfo(exportPath)))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1");
worksheet.Cells[1,1].Value="Prefab";
worksheet.Cells [1, 2].Value = "個數(shù)";
worksheet.Cells [1, 3].Value = "單價";
worksheet.Cells [1, 4].Value = "總價";
package.Save ();
}
}
其他參考
設置整個表格的默認字體
worksheet.Cells.Style.Font.Size = 11; //Default font size for whole sheet
worksheet.Cells.Style.Font.Name = "Calibri"; //Default Font name for whole sheet
單元格賦值
worksheet.Cells["A1"].Value = 1; //設置A1單元格的值為1
worksheet.Cells[1,1].Value = 1;//設置A1單元格的值為1
設置單元格數(shù)字格式
worksheet.Cells["A1:B3"].Style.NumberFormat.Format = "#,##0";
worksheet.Cells[1,1,3,2].Style.NumberFormat.Format = "#,##0";
設置單元格背景色
var fill = cell.Style.Fill;
fill.PatternType = ExcelFillStyle.Solid;
fill.BackgroundColor.SetColor(Color.Gray);
添加Image
private static void AddImage(ExcelWorksheet ws, int columnIndex, int rowIndex, string filePath)
{
//How to Add a Image using EP Plus
Bitmap image = new Bitmap(filePath);
ExcelPicture picture = null;
if (image != null)
{
picture = ws.Drawings.AddPicture("pic" + rowIndex.ToString() + columnIndex.ToString(), image);
picture.From.Column = columnIndex;
picture.From.Row = rowIndex;
picture.From.ColumnOff = Pixel2MTU(2); //Two pixel space for better alignment
picture.From.RowOff = Pixel2MTU(2);//Two pixel space for better alignment
picture.SetSize(100, 100);
}
}
更多信息 可以參考官方文檔 http://epplus.codeplex.com/wikipage?title=FAQ&referringTitle=Documentation