unity中将Text搭建的表格导出Excel表

unity中通过Text组件搭建的表格如下图所示

备注:每一行是一个prefab,每一个单元格是一个Text

unity中将Text搭建的表格导出Excel表_第1张图片

具体代码如下

使用的Excel相关插件请查看  此博客  https://blog.csdn.net/U3DCoder/article/details/82867155

   //点击按钮导出报表
   reportui.transform.FindChild("energyReportInfo/energyReportitle-top/Button/DownloadBtn").GetComponent().onClick.AddListener(() =>
    {
            
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "/数据报表/数据表";
           
            MessageBox.Show("报表路径:" + dir);
            SaveExcel(dir, "数据报表.xlsx");
            WriteExcel(dir + "/数据报表.xlsx");

            
    });


 //创建Excel表
    public static void SaveExcel(string path, string fileName)
    {
        if (!System.IO.Directory.Exists(path))
        {
            System.IO.Directory.CreateDirectory(path);
        }
        if (System.IO.File.Exists(path + "\\" + fileName))
        {
            System.IO.File.Delete(path + "\\" + fileName);
        }
        System.IO.File.Create(path + "\\" + fileName).Close();
    }
    //导出能耗数据Excel表,将数据写入Excel中
    int energyRow = 2;
    int energyCloumn = 1;
    public void WriteExcel(string outputDir)
    {
        FileInfo newFile = new FileInfo(outputDir);
        if (newFile.Exists)
        {
            newFile.Delete();  // ensures we create a new workbook
            newFile = new FileInfo(outputDir);
        }

        using (ExcelPackage package = new ExcelPackage(newFile))
        {
            // add a new worksheet to the empty workbook
            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1");
            //Add the headers
            worksheet.Cells[1, 1].Value = "项目名称";
            worksheet.Cells[1, 2].Value = "1线";
            worksheet.Cells[1, 3].Value = "2线";
            worksheet.Cells[1, 4].Value = "3线";
            worksheet.Cells[1, 5].Value = "4线";
            worksheet.Cells[1, 6].Value = "5线";
            worksheet.Cells[1, 7].Value = "6线";
            worksheet.Cells[1, 8].Value = "8台(k4)";
            worksheet.Cells[1, 9].Value = "8台(K1)";
            worksheet.Cells[1, 10].Value = "3台";
            worksheet.Cells[1, 11].Value = "12台K2";
            worksheet.Cells[1, 12].Value = "9台(K3)";
            worksheet.Cells[1, 13].Value = "总计";

            foreach (Transform kk in reportui.transform.FindChild("energyReportInfo/bg/energyList/Content"))
            {
                Transform temp = kk;
                foreach (Transform cc in temp.transform.FindChild("content"))
                {
                    worksheet.Cells[energyRow, energyCloumn].Value = cc.GetComponent().text;
                    //Debug.LogError(energyRow + "  数据 " + energyCloumn + "  " + cc.GetComponent().text);
                    if (energyCloumn < temp.transform.FindChild("content").childCount)
                    {
                        energyCloumn++;
                    }
                    else
                    {
                        energyCloumn = 1;
                    }

                }
                if (energyRow < reportui.transform.FindChild("energyReportInfo/bg/energyList/Content").childCount+1)
                {
                    energyRow++;
                }
                else 
                {
                    energyRow=2;
                }
              
            }

            //foreach (var kk in m_nameToGameObject.Values)
            //{
            //    // Debug.LogError("存储的值    " + kk);
            //}
            //Debug.LogError("数量 " + reportui.transform.FindChild("energyReportInfo/bg/energyList/Content").childCount);

            //for (i = 2; i <= reportui.transform.FindChild("energyReportInfo/bg/energyList/Content").childCount + 1; i++)
            //{
            //    for (j = 1; j <= 13; j++)
            //    {
            //        //Debug.LogError(i + "      " + j);
            //        //foreach (Transform kk in reportui.transform.FindChild("energyReportInfo/bg/energyList/Content"))
            //        //{
            //        //    Transform temp = kk;
            //        //    foreach (Transform cc in temp.transform.FindChild("content"))
            //        //    {
            //        //        Debug.LogError(i + "   " + j+  "  "+cc.GetComponent());
            //        //    }
            //        //}
            //        foreach (var kk in m_nameToGameObject)
            //        {

            //            //if (kk.Key == kk.Value.name)
            //            //{
            //            //    Debug.LogError("存储的值    " + kk.Value.GetComponent().text);
            //            //}
            //            //energyData = kk.Value.GetComponent().text;

            //            //Debug.LogError(i + "   " + j + "  " + energyData);
            //        }
            //        //worksheet.Cells[i, j].Value = ;

            //    }
            //}



            //Add some items...
            //worksheet.Cells["A2"].Value = 12001;
            //worksheet.Cells["B2"].Value = "Nails";
            //worksheet.Cells["C2"].Value = 37;
            //worksheet.Cells["D2"].Value = 3.99;

            //worksheet.Cells["A3"].Value = 12002;
            //worksheet.Cells["B3"].Value = "Hammer";
            //worksheet.Cells["C3"].Value = 5;
            //worksheet.Cells["D3"].Value = 12.10;

            //worksheet.Cells["A4"].Value = 12003;
            //worksheet.Cells["B4"].Value = "Saw";
            //worksheet.Cells["C4"].Value = 12;
            //worksheet.Cells["D4"].Value = 15.37;

            //save our new workbook and we are done!
            package.Save();
        }
    }

 

 

 

 

 

你可能感兴趣的:(Unity开发,Unity,UGUI)