簡體   English   中英

使用 C# 中的 ExcelLibrary 創建具有多個工作表的多個 Excel

[英]create multiple excels with multiple sheet using ExcelLibrary in C#

在此處輸入圖像描述

我想用多個 excel 文件將數據導出到多個工作表中。 正如您在我的圖像中看到的,我想在ID更改時生成工作表,當ModalityId更改時,我想創建新的 excel。

為此,我編寫了這樣的代碼:

List<string> lstFile = new List<string>();
if (dtAltTag != null && dtAltTag.Rows.Count > 0)
{
    string filePath = string.Empty;
    string fileName = "";

    Excel.Workbook workBook = new Excel.Workbook();
    var workSheet = new Excel.Worksheet(fileName);

    if (dtAltTag.Rows[0]["OldFormCode"] != null && dtAltTag.Rows[0]["OldFormCode"].ToString() != "")
    {
        fileName = dtAltTag.Rows[0]["OldFormCode"].ToString();
    }
    else
    {
        fileName = dtAltTag.Rows[0]["Code"].ToString();
    }
    workSheet.Name = fileName;
    AddValue(0, workSheet, dtAltTag); // function is used to add the value in the sheet
    workBook.Worksheets.Add(workSheet);

    //data working
    for (int i = 0; i < dtAltTag.Rows.Count; i++)
    {
        //for modality changes and first entery 
        //if (i == 0 || dtAltTag.Rows[i]["ModalityId"] != dtAltTag.Rows[i - 1]["ModalityId"])
        //{
        //if form changes then it should be in other sheet
        if (i != 0 && dtAltTag.Rows[i]["ID"].ToString() != dtAltTag.Rows[i - 1]["ID"].ToString())
        {
            if (dtAltTag.Rows[i]["OldFormCode"] != null && dtAltTag.Rows[i]["OldFormCode"].ToString() != "")
            {
                fileName = dtAltTag.Rows[i]["OldFormCode"].ToString();
            }
            else
            {
                fileName = dtAltTag.Rows[i]["Code"].ToString();
            }
            var workSheet1 = new Excel.Worksheet(fileName);

            AddValue(i, workSheet1, dtAltTag);
        }
        else
        {

        }
    }

    filePath = HostingEnvironment.MapPath(ConfigurationManager.AppSettings["ExcelFilesFilePath"]) + "Allitem";
    if (!File.Exists(filePath))
        Directory.CreateDirectory(filePath);
    filePath = filePath + "\\" + fileName + "_" + DateTime.Now.ToString("MM_dd_yyy_HH_mm_ss") + ".xls";
    workBook.Save(filePath);
    lstFile.Add(filePath);
}
return lstFile;

當 id 更改時,我 append new header 但之后,我想繼續導出數據,直到 id 更改無法檢測到如何執行此操作? 在其他情況下,如何獲取我繼續添加值的當前工作表?

我希望你清楚我想要做什么!

提前致謝!

你不能這樣做嗎:

  • 循環數據
  • 如果ModalityId不同,則保存當前文件並創建一個新文件(將其分配回當前文件變量)
  • 如果ID不同,則創建一個新工作表(將其附加到當前文件並將其分配回當前工作表變量)
  • 將您的數據添加到當前工作表
  • 在循環之外,保存當前(和最后一個)文件。

這可能是它的偽代碼

var currentFile = new ExcelFile();
var currentSheet = currentFile.AppendSheet(rows[0].SheetName);
string lastFileName;

for(int i = 0; i < rows.Count; i++)
{
    // Adds new file if necessary
    if(i != 0 && rows[i].ModalityId != rows[i - 1].ModalityId)
    {
        currentFile.Save(rows[i - 1].FileName);
        currentFile = new ExcelFile();
    }
    // Adds new sheet if necessary
    if(i != 0 && rows[i].ID != rows[i - 1].ID)
    {
        currentSheet = currentFile.AppendSheet(rows[i].SheetName);
    }

    InsertData(currentSheet, rows[i]);

    lastFileName = rows[i].FileName;
}

currentFile.Save(lastFileName);

參考“Rafalon”代碼,我對代碼進行了一些更改,並且運行良好! 謝謝你!

我的代碼如下:

 var filePath = "";
            string fileName = "";

            if (dtAltTag.Rows[0]["OldFormCode"] != null && dtAltTag.Rows[0]["OldFormCode"].ToString() != "")
            {
                fileName = dtAltTag.Rows[0]["OldFormCode"].ToString();
            }
            else
            {
                fileName = dtAltTag.Rows[0]["Code"].ToString();
            }

            int Row = 0;
            var currentFile = new Excel.Workbook();
            var currentSheet = new Excel.Worksheet(fileName);
            currentFile.Worksheets.Add(currentSheet);

            List<string> lstFile = new List<string>();
            if (dtAltTag != null && dtAltTag.Rows.Count > 0)
            {


                for (int i = 0; i < dtAltTag.Rows.Count; i++)
                {

                    if (i == 0)
                    {
                        AddValue(i, currentSheet, dtAltTag, Row);
                    }
                    else
                    {
                        if (dtAltTag.Rows[i]["ModalityId"].ToString() != dtAltTag.Rows[i - 1]["ModalityId"].ToString())
                        {
                            filePath = HostingEnvironment.MapPath(ConfigurationManager.AppSettings["ExcelFilesFilePath"]) + "Allitem";
                            if (!File.Exists(filePath))
                                Directory.CreateDirectory(filePath);
                            filePath = filePath + "\\" + fileName + "_" + DateTime.Now.ToString("MM_dd_yyy_HH_mm_ss") + ".xls";
                            lstFile.Add(filePath);
                            currentFile.Save(filePath);
                            currentFile = new Excel.Workbook();

                        }
                        if (dtAltTag.Rows[i]["ID"].ToString() != dtAltTag.Rows[i - 1]["ID"].ToString())
                        {

                            if (dtAltTag.Rows[i]["OldFormCode"] != null && dtAltTag.Rows[i]["OldFormCode"].ToString() != "")
                            {
                                fileName = dtAltTag.Rows[i]["OldFormCode"].ToString();
                            }
                            else
                            {
                                fileName = dtAltTag.Rows[i]["Code"].ToString();
                            }
                            currentSheet = new Excel.Worksheet(fileName);
                            currentFile.Worksheets.Add(currentSheet);
                            Row = 0;

                        }

                        AddValue(i, currentSheet, dtAltTag, Row);


                    }
                    Row++;

                }


                filePath = HostingEnvironment.MapPath(ConfigurationManager.AppSettings["ExcelFilesFilePath"]) + "Allitem";
                if (!File.Exists(filePath))
                    Directory.CreateDirectory(filePath);
                filePath = filePath + "\\" + fileName + "_" + DateTime.Now.ToString("MM_dd_yyy_HH_mm_ss") + ".xls";           
                currentFile.Save(filePath);
                lstFile.Add(filePath);

            }

這個 function 在工作表中增加價值可能有用:

private void AddValue(int i, Excel.Worksheet workSheet, DataTable dtAltTag, int Row)
        {
            //Header Working starts here
            string[] columName = new string[] { "column1", "column2", "column2", "column2", "column2" }; //add header as per you requirement
            if (Row == 0)
            {
                for (int c = 0; c < columName.Length; c++)
                {
                    workSheet.Cells[0, c] = new Excel.Cell(columName[c]);
                }
            }

            //add data
            for (int c = 0; c < columName.Length; c++)
            {
                workSheet.Cells[Row + 1, c] = new Excel.Cell(dtAltTag.Rows[i].ItemArray[c].ToString());            
            }


        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM