Unity3D 編輯器 -導出Excel文件

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容