Unity3D 编辑器 -导出Excel文件

EPPlus 是使用Open Office XML格式(xlsx)读写Excel 2007 / 2010文件的.net开发库。
官网:http://epplus.codeplex.com/

基于EEPlus在Unity编辑器中导出Excel文件。

Step1:

到官网 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 = "个数";
        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
设置单元格数字格式
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

你可能感兴趣的:(Unity3D 编辑器 -导出Excel文件)