从excel文档中获取页脚

时间:2014-09-09 14:49:06

标签: c# excel

如何使用C#获取Excel文档的页脚?下面是我用来检索页脚的代码。

string sFooterExcel = string.Empty;
xlsApp = new Excel.ApplicationClass();
xlsWorkbook = xlsApp.Workbooks.Open(sPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
 int i = xlsWorkbook.Worksheets.Count;
 foreach (Excel.Worksheet sheet in xlsWorkbook.Worksheets)
 {
  sFooterExcel = sheet.PageSetup.LeftFooter;
            sFooterExcel += sFooterExcel + sheet.PageSetup.CenterFooter;
            sFooterExcel += sFooterExcel + sheet.PageSetup.RightFooter;
        }
        return sFooterExcel;
    }

但我得到的字体数据如下:

"&"Trebuchet MS,Bold"&10 Confidential&"Trebuchet MS,Bold"&10 Confidential"

我只想' Confidential' 作为我的页脚。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

您应该手动剥离格式。您可以在http://msdn.microsoft.com/en-us/library/ff822794(v=office.15).aspx找到格式代码。

例如,正则表达式可以剥离它:

private static string TrimFormatting(string headerOrFooter)
{
    return Regex.Replace(headerOrFooter, "&L|&C|&R|&E|&X|&Y|&B|&I|&U|&S|&\"[^\"]+\"|&(\\d\\d|\\d)|&K[0-9a-fA-F]{6}|&K\\d\\d[\\+\\-]\\d\\d\\d", string.Empty);
}

然后您可以使用此方法获取页脚的文本

sFooterExcel = TrimFormatting(sheet.PageSetup.LeftFooter);
sFooterExcel += TrimFormatting(sheet.PageSetup.CenterFooter);
sFooterExcel += TrimFormatting(sheet.PageSetup.RightFooter);