请问各位大虾,在C# winform中怎样设置excel的 页边距,代码怎样写?
2个回答
展开全部
using Excel = Microsoft.Office.Interop.Excel;
/// <summary>
/// 页面设置
/// </summary>
/// <param name="xlPagerSize">页面大小</param>
/// <param name="xlPageOrientation">垂直还是水平</param>
/// <param name="headerMargin">页眉大小</param>
/// <param name="footerMargin">页脚大小</param>
/// <param name="leftMargin">左边距</param>
/// <param name="rightMargin">右边距</param>
/// <param name="topMargin">上边距</param>
/// <param name="bottomMargin">下边距</param>
public void PageSetup(XlPaperSize xlPagerSize, XlPageOrientation xlPageOrientation, double headerMargin, double footerMargin, double leftMargin, double rightMargin, double topMargin, double bottomMargin)
{
Excel.Worksheet worksheet = (Excel.Worksheet)myExcel.ActiveSheet;
worksheet.PageSetup.PaperSize = xlPagerSize;
worksheet.PageSetup.Orientation = xlPageOrientation;
worksheet.PageSetup.HeaderMargin = headerMargin;
worksheet.PageSetup.FooterMargin = footerMargin;
worksheet.PageSetup.LeftMargin = leftMargin;
worksheet.PageSetup.RightMargin = rightMargin;
worksheet.PageSetup.TopMargin = myExcel.InchesToPoints(topMargin);
worksheet.PageSetup.BottomMargin = myExcel.InchesToPoints(bottomMargin);
worksheet.PageSetup.CenterHorizontally = true;
}
/// <summary>
/// 页面设置
/// </summary>
/// <param name="xlPagerSize">页面大小</param>
/// <param name="xlPageOrientation">垂直还是水平</param>
/// <param name="headerMargin">页眉大小</param>
/// <param name="footerMargin">页脚大小</param>
/// <param name="leftMargin">左边距</param>
/// <param name="rightMargin">右边距</param>
/// <param name="topMargin">上边距</param>
/// <param name="bottomMargin">下边距</param>
public void PageSetup(XlPaperSize xlPagerSize, XlPageOrientation xlPageOrientation, double headerMargin, double footerMargin, double leftMargin, double rightMargin, double topMargin, double bottomMargin)
{
Excel.Worksheet worksheet = (Excel.Worksheet)myExcel.ActiveSheet;
worksheet.PageSetup.PaperSize = xlPagerSize;
worksheet.PageSetup.Orientation = xlPageOrientation;
worksheet.PageSetup.HeaderMargin = headerMargin;
worksheet.PageSetup.FooterMargin = footerMargin;
worksheet.PageSetup.LeftMargin = leftMargin;
worksheet.PageSetup.RightMargin = rightMargin;
worksheet.PageSetup.TopMargin = myExcel.InchesToPoints(topMargin);
worksheet.PageSetup.BottomMargin = myExcel.InchesToPoints(bottomMargin);
worksheet.PageSetup.CenterHorizontally = true;
}
追问
谢谢你!
另外我还想问一下,我想在excel工作簿中建立两个工作表,用C#代码怎么写
追答
Excel.Worksheet newWorksheet;
newWorksheet = (Excel.Worksheet)Globals.ThisWorkbook.Worksheets.Add(
missing, missing, missing, missing);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询