Unity 生成 Excel表格

Unity生成Excel需要EPPlus这个库,Editor状态可以使用,打包后需要加入 I18N.CJK  和 I18N 这两个库

代码如下:

    private void SaveExcel()
    {
        OpenFileName ofn = new OpenFileName();

        ofn.structSize = Marshal.SizeOf(ofn);

        //ofn.filter = "All Files\0*.*\0\0";
        //ofn.filter = "Image Files(*.jpg;*.png)\0*.jpg;*.png\0";
        //ofn.filter = "Txt Files(*.txt)\0*.txt\0";

        //ofn.filter = "Word Files(*.docx)\0*.docx\0";
        //ofn.filter = "Word Files(*.doc)\0*.doc\0";
        //ofn.filter = "Word Files(*.doc:*.docx)\0*.doc:*.docx\0";

        //ofn.filter = "Excel Files(*.xls)\0*.xls\0";
        ofn.filter = "Excel Files(*.xlsx)\0*.xlsx\0";  //指定打开格式
        //ofn.filter = "Excel Files(*.xls:*.xlsx)\0*.xls:*.xlsx\0";
        //ofn.filter = "Excel Files(*.xlsx:*.xls)\0*.xlsx:*.xls\0";

        ofn.file = new string(new char[256]);

        ofn.maxFile = ofn.file.Length;

        ofn.fileTitle = new string(new char[64]);

        ofn.maxFileTitle = ofn.fileTitle.Length;

        ofn.initialDir = UnityEngine.Application.dataPath;//默认路径

        ofn.title = "打开Excel";

        ofn.defExt = "xlsx";

        //注意 一下项目不一定要全选 但是0x00000008项不要缺少
        ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR


        //打开windows框
        if (DllTest.GetSaveFileName(ofn))
        {
            //TODO

            //把文件路径格式替换一下
            ofn.file = ofn.file.Replace("\\", "/");
            Debug.Log(ofn.file);

            FileInfo newFile = new FileInfo(ofn.file);
            if (newFile.Exists)
            {
                newFile.Delete();  // ensures we create a new workbook
                newFile = new FileInfo(ofn.file);
            }
            using (ExcelPackage package = new ExcelPackage(newFile))
            {
                // 添加一个sheet
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("信息");

                //添加一点测试数据
                List<ImportClass> importClasses = new List<ImportClass>();
                importClasses.Add(new ImportClass("小刚", "789", "789789"));
                importClasses.Add(new ImportClass("小亮", "147", "147147"));

                worksheet.Cells[1, 1, 1, 6].Merge = true;//合并单元格(1行1列到1行6列)
                worksheet.Cells["A1"].Value = "学生信息"; //显示
                worksheet.Cells["A1"].Style.Font.Size = 16; //字体大小
                worksheet.Cells["A1"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; //对其方式
                worksheet.Cells["A1"].Style.Border.BorderAround(ExcelBorderStyle.Thin); //表格边框


                worksheet.Cells[2, 1, 2, 2].Merge = true;
                worksheet.Cells["A2"].Value = "姓名";
                worksheet.Cells["A2"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                worksheet.Cells["A2"].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                worksheet.Cells["B2"].Style.Border.BorderAround(ExcelBorderStyle.Thin);

                worksheet.Cells[2, 3, 2, 4].Merge = true;
                worksheet.Cells["C2"].Value = "学号";
                worksheet.Cells["C2"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                worksheet.Cells["C2"].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                worksheet.Cells["D2"].Style.Border.BorderAround(ExcelBorderStyle.Thin);

                worksheet.Cells[2, 5, 2, 6].Merge = true;
                worksheet.Cells["E2"].Value = "密码";
                worksheet.Cells["E2"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                worksheet.Cells["E2"].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                worksheet.Cells["F2"].Style.Border.BorderAround(ExcelBorderStyle.Thin);


                int i = 3;//第三行才是我们的数据
                for (int j = 0; j < importClasses.Count; j++)
                {
                    worksheet.Cells[i, 1, i, 2].Merge = true;
                    worksheet.Cells["A" + i.ToString()].Value = importClasses[j].nickname;
                    worksheet.Cells["A" + i.ToString()].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    worksheet.Cells["A" + i.ToString()].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    worksheet.Cells["B" + i.ToString()].Style.Border.BorderAround(ExcelBorderStyle.Thin);

                    worksheet.Cells[i, 3, i, 4].Merge = true;
                    worksheet.Cells["C" + i.ToString()].Value = importClasses[j].studentid;
                    worksheet.Cells["C" + i.ToString()].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    worksheet.Cells["C" + i.ToString()].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    worksheet.Cells["D" + i.ToString()].Style.Border.BorderAround(ExcelBorderStyle.Thin);

                    worksheet.Cells[i, 5, i, 6].Merge = true;
                    worksheet.Cells["E" + i.ToString()].Value = importClasses[j].password;
                    worksheet.Cells["E" + i.ToString()].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    worksheet.Cells["E" + i.ToString()].Style.Border.BorderAround(ExcelBorderStyle.Thin);
                    worksheet.Cells["F" + i.ToString()].Style.Border.BorderAround(ExcelBorderStyle.Thin);

                    i++;
                }

                package.Save();
            }
        }
    }

最终生成的excel是这样的:

 

上面关于表格布局的代码可以用更优雅的方式,我这里只是做个测试

链接:https://pan.baidu.com/s/1rOiK1Nx4zkJMTx5F3HWNRA  提取码:17ac

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值