在Excel工作簿中交换工作表c#

时间:2016-09-16 08:13:32

标签: c# asp.net excel swap worksheet

例如,请参阅图片

enter image description here

我想交换工作表" Sheet1"到" Sheet3"。

使用EPPlus的我的代码:

if (num1 == 5)
{
    Console.WriteLine("\nThe " + num2 + " kilo/s of {0} " + 28 + " per kilo ", "GRAPES");
    Console.WriteLine("The total amount is {0}{1}", num2.ToString("en-PHI"),num2*28);
}
num2.ToString("en-PHI")

怎么做?有任何建议请..

2 个答案:

答案 0 :(得分:0)

如果只需要交换工作表(我的意思是不需要处理内容),那么重命名工作表应该很简单。

  1. 重命名" Sheet1"到" adsf"
  2. 重命名" Sheet3"到" Sheet1"
  3. 重命名" adsf"到" Sheet3"
  4. 表格(" Sheet1")。姓名=" adsf"

    表格(" Sheet3")。姓名=" Sheet1"

    表格(" adsf")。姓名=" Sheet3"

答案 1 :(得分:0)

这很好用:

ExcelPackage masterPackage = new ExcelPackage();
foreach (var file in files)
{
    ExcelPackage pckg = new ExcelPackage(new FileInfo(file));
    foreach (var sheet in pckg.Workbook.Worksheets)
    {
        //check name of worksheet, in case that worksheet with same name already exist exception will be thrown by EPPlus
        string workSheetName = sheet.Name;
        foreach (var masterSheet in masterPackage.Workbook.Worksheets)
        {
            if (sheet.Name == masterSheet.Name)
            {
                workSheetName = string.Format("{0}_{1}", workSheetName, DateTime.Now.ToString("yyyyMMddhhssmmm"));
            }
        }

        //add new sheet
        if (sheet.Name.Contains("MB_STORE_POTENTIALvsWALLET"))
        {
             masterPackage.Workbook.Worksheets.Add(workSheetName, sheet);
        }
        else
        {
             masterPackage.Workbook.Worksheets.Add(workSheetName, sheet);
             masterPackage.Workbook.Worksheets.MoveBefore(2, 1);
        }
    }
}
masterPackage.SaveAs(new FileInfo(resultFile));