C#操作Excel時的格式設定(轉)


Excel報表打印的格式設定

1.     表頭的設置

Excel._Worksheet myWorksheet;

   myWorksheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;

//紙張方向, 返回或者設置對象的方向, 縱向或橫向打印模式

//Excel.XlPageOrientation.xlLandscape   landscape mode  :worksheet橫幅

//Excel.XlPageOrientation.xlPortrait    Portrait  mode  :chart豎幅A4紙

myWorksheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4;//紙張大小

 

xlPaper10x14

 

10 in. x 14 in.

 

xlPaper11x17

 

11 in. x 17 in.

 

xlPaperA3

 

A3 (297 mm x 420 mm)

 

xlPaperA4

 

A4 (210 mm x 297 mm)

 

xlPaperA4Small

 

A4 Small (210 mm x 297 mm)

 

xlPaperA5

 

A5 (148 mm x 210 mm)

 

xlPaperB4

 

B4 (250 mm x 354 mm)

 

xlPaperB5

 

A5 (148 mm x 210 mm)

 

xlPaperCsheet

 

C size sheet

 

xlPaperDsheet

 

D size sheet

 

xlPaperEnvelope10

 

Envelope #10 (4-1/8 in. x 9-1/2 in.)

 

xlPaperEnvelope11

 

Envelope #11 (4-1/2 in. x 10-3/8 in.)

 

xlPaperEnvelope12

 

Envelope #12 (4-1/2 in. x 11 in.)

 

xlPaperEnvelope14

 

Envelope #14 (5 in. x 11-1/2 in.)

 

xlPaperEnvelope9

 

Envelope #9 (3-7/8 in. x 8-7/8 in.)

 

xlPaperEnvelopeB4

 

Envelope B4 (250 mm x 353 mm)

 

xlPaperEnvelopeB5

 

Envelope B5 (176 mm x 250 mm)

 

xlPaperEnvelopeB6

 

Envelope B6 (176 mm x 125 mm)

 

xlPaperEnvelopeC3

 

Envelope C3 (324 mm x 458 mm)

 

xlPaperEnvelopeC4

 

Envelope C4 (229 mm x 324 mm)

 

xlPaperEnvelopeC5

 

Envelope C5 (162 mm x 229 mm)

 

xlPaperEnvelopeC6

 

Envelope C6 (114 mm x 162 mm)

 

xlPaperEnvelopeC65

 

Envelope C65 (114 mm x 229 mm)

 

xlPaperEnvelopeDL

 

Envelope DL (110 mm x 220 mm)

 

xlPaperEnvelopeItaly

 

Envelope (110 mm x 230 mm)

 

xlPaperEnvelopeMonarch

 

Envelope Monarch (3-7/8 in. x 7-1/2 in.)

 

xlPaperEnvelopePersonal

 

Envelope (3-5/8 in. x 6-1/2 in.)

 

xlPaperEsheet

 

E size sheet

 

xlPaperExecutive

 

Executive (7-1/2 in. x 10-1/2 in.)

 

xlPaperFanfoldLegalGerman

 

German Legal Fanfold (8-1/2 in. x 13 in.)

 

xlPaperFanfoldStdGerman

 

German Legal Fanfold (8-1/2 in. x 13 in.)

 

xlPaperFanfoldUS

 

U.S. Standard Fanfold (14-7/8 in. x 11 in.)

 

xlPaperFolio

 

Folio (8-1/2 in. x 13 in.)

 

xlPaperLedger

 

Ledger (17 in. x 11 in.)

 

xlPaperLegal

 

Legal (8-1/2 in. x 14 in.)

 

xlPaperLetter

 

Letter (8-1/2 in. x 11 in.)

 

xlPaperLetterSmall

 

Letter Small (8-1/2 in. x 11 in.)

 

xlPaperNote

 

Note (8-1/2 in. x 11 in.)

 

xlPaperQuarto

 

Quarto (215 mm x 275 mm)

 

xlPaperStatement

 

Statement (5-1/2 in. x 8-1/2 in.)

 

xlPaperTabloid

 

Tabloid (11 in. x 17 in.)

 

xlPaperUser

 

User-defined

 

myWorksheet.PageSetup.Zoom = false;  //返回或者設置一個百分比(數值在 10% 和 400% 之間),該百分比為 Microsoft Excel 打印指定工作表時的縮放比例. 如果本屬性設為 False,則由 FitToPagesWide 屬性和 FitToPagesTall 屬性的設定值對工作表的縮放進行控制

myWorksheet.PageSetup.FitToPagesWide = 1;

返回或者設置打印工作表時,對工作表進行縮放使用的頁寬。僅應用於工作表。如果本屬性設為 False,則 Microsoft 根據 FitToPagesTall 屬性的設置對工作表進行縮放。

如果 Zoom 屬性設為 True,則忽略 FitToPagesWide 屬性。 Eg:本示例設置 Microsoft Excel 恰好按照一頁的寬度和高度打印 Sheet1。

With Worksheets("Sheet1").PageSetup.

    Zoom = False

    FitToPagesTall = 1.

    FitToPagesWide = 1

End With

myWorksheet.PageSetup.FitToPagesTall = false;

返回或者設置打印工作表時,對工作表進行縮放使用的頁高。僅應用於工作表。如果本屬性設為 False,則 Microsoft Excel 根據 FitToPagesWide 屬性的設置對工作表進行縮放。 如果 Zoom 屬性設為 True,則忽略 FitToPagesTall 屬性。

     //C#

     myWorksheet.PageSetup.CenterHeader=@"&""Helv,Bold""&18" + ERP.Model.Common.LoginInfo.userLoginInfo.companyName + "訂單庫存" + "\n日期別訂單未出數量明細表";

    輸出一個雙引號:Console.WriteLine(@””””);

     在@后的字串中,兩個雙引號相當於一個雙引號

     字體名字:

           Arial

          Courier

          Garamond

          Time New Roman

          Verdana

          Helv

 

     //VB

With xlSheet.PageSetup

      .LeftHeader = "" & Chr(10) & "&""楷體_GB2312,常規""&10公司名稱:"   ' & Gsmc

       .CenterHeader = "&""楷體_GB2312,常規""公司人員情況表&""宋體,常規""" & Chr(10) & "&""楷體_GB2312,常規""&10日 期:"

       .RightHeader = "" & Chr(10) & "&""楷體_GB2312,常規""&10單位:"

       .LeftFooter = "&""楷體_GB2312,常規""&10制表人:"

       .CenterFooter = "&""楷體_GB2312,常規""&10制表日期:"

       .RightFooter = "&""楷體_GB2312,常規""&10第&P頁 共&N頁"

End With

myWorksheet.PageSetup.CenterFooter=@"頁次: &P of &N";

myWorksheet.PageSetup.LeftMargin=25.6;//左邊距

myWorksheet.PageSetup.RightMargin=16;//右邊距

myWorksheet.PageSetup.TopMargin=85.5;//上邊距

myWorksheet.PageSetup.BottomMargin=33;//下邊距 myWorksheet.PageSetup.HeaderMargin=41.2;//頁眉

myWorksheet.PageSetup.FooterMargin=12.8;//頁腳

myWorksheet.PageSetup.CenterHorizontally=true; //水平居中

 

myRange.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle=Excel.XlLineStyle.xlContinuous;//邊框線的類型

//XlBordersIndex enum:指定某個范圍的某個邊框

 

Member name

 

Description

 

xlDiagonalDown

 

Border running from the upper left-hand corner to the lower right of each cell in the range. 設置斜向下邊框

 

xlDiagonalUp

 

Border running from the lower left-hand corner to the upper right of each cell in the range. 設置斜向上邊框

 

xlEdgeBottom

 

Border at the bottom of the range. 設置底邊框

 

xlEdgeLeft

 

Border at the left-hand edge of the range. 設置左邊框

 

xlEdgeRight

 

Border at the right-hand edge of the range. 設置右邊框

 

xlEdgeTop

 

Border at the top of the range. 設置頂邊框

 

xlInsideHorizontal

 

Horizontal borders for all cells in the range except

borders on the outside of the range. 設置水平邊框

 

xlInsideVertical

 

Vertical borders for all the cells in the range except borders on the outside of the range設置垂直邊框

 

xlInsideVertical, xlInsideHorizontal

 

設置中間的十字框

 

XlLineStyle Enumeration:指定邊框線的類型

 

xlContinuous          

 

Continuous line.連續的線條

 

xlDash

 

Dashed line.虛線

 

xlDashDot

 

Alternating dashes and dots. 點虛線

 

xlDashDotDot

 

Dash followed by two dots.

 

xlDot

 

Dotted line.點線

 

xlDouble

 

Double line.雙線

 

xlLineStyleNone

 

No line.沒有線.// 設置為沒有邊框

 

xlSlantDashDot

 

Slanted dashes. 斜線

 

 

myRange.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlMedium;

// Specifies the weight of the border around a range

//XlBorderWeight Enumeration:指定線的粗細程度

 

xlHairline

 

Hairline (thinnest border).最細

 

xlMedium

 

Medium.中等

 

xlThick

 

Thick (widest border).粗

 

xlThin

 

Thin.細                     

 

myRange.Borders[Excel.XlBordersIndex.xlEdgeTop].ColorIndex

=Excel.XlColorIndex.xlColorIndexAutomatic;

//ColorIndex Enumeration:

Specifies the color of a selected feature such as border, font, or fill.

 

xlColorIndexAutomatic

 

Automatic color.

 

xlColorIndexNone

 

No color.

 

本屬性將一種顏色指定為工作簿調色板的一條索引。可以使用 Colors 方法返回當前的調色板。以下示范顯示默認調色板中的顏色索引值。

myWorksheet.PageSetup.PrintTitleRows="$1:$3";

//設置頂端標題行,// 設置打印固定行//設定每一頁必打印的行//返回或設置那些包含在每一頁頂部重復出現的單元格的行,用宏語言字符串以 A1-樣式的記號表示, String 類型,可讀寫

//VB

oExl.ActiveSheet.PageSetup.PrintTitleRows ="$1:$2"   &&設置頂端標題行

myWorksheet.PageSetup. PrintTitleColumns="$A:$C" && 定義固定列返回或設置包含在每一頁的左邊重復出現單元格的列,用宏語言中 A1-樣式的字符串記號,String 類型,可讀寫。

Excel.Range myRange = myWorksheet.get_Range(strStartPoint,strEndPoint);

(1)myRange.MergeCells = true;如果區域或樣式包含合並單元格,本屬性為 True

(2)  myRange.Merge(object Across);

//Creates a merged cell from the specified Range object

Across : Optional Object. True to merge cells in each row of the specified range as separate merged cells. The default value is False. The value of a merged range is specified in the cell of the range's upper-left corner.

(3)myRange.MergeArea//Returns a Range object that represents the merged range containing the specified cell.If the specified cell isn’t in a merged range, this property returns the specified cell.The MergeArea property only works on a single-cell rang返回 Range 對象,代表包含指定單元格的合並的范圍。如果指定的單元格不在合並的范圍內,則該屬性返回指定的單元格。只讀。Variant類型。

myRange.VerticalAlignment = Excel.XlVAlign.xlVAlignTop; 返回或設置指定對象的垂直對齊方式

XlVAlign enum: Specifies the vertical alignment for the object.

 

NamedRange 控件中的文本進行換行。如果 Excel 對該對象中的文本進行換行,則為 true;如果 NamedRange 控件包含一些對文本進行換行的單元格,還包含一些不對文本進行換行的單元格,則為 空引用. Excel 在必要時將更改 NamedRange 控件的行高以容納該范圍中的文本。

myRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;// 返回或設置指定對象的水平對齊方式// XlHAlign enum:Specifies the horizontal alignment for the object.

 

xlHAlignCenter

 

Center. 

 

xlHAlignCenterAcrossSelection

 

Center across selection.

 

xlHAlignDistributed

 

Distribute.

 

xlHAlignFill

 

Fill.

 

xlHAlignGeneral

 

Align according to data type.

 

xlHAlignJustify

 

Justify.

 

xlHAlignLeft

 

Left.

 

xlHAlignRight

 

Right.

 

 

 

2. 代碼對頁眉和頁腳格式

在表頭中使用多行,請用以下兩種方法之一:L用CHR(10)插入換行符;LCHR(13)插入回車符

注意您無法記錄宏中這些字符。下列表包含格式代碼, 頁眉和頁腳中使用。

   Code to format Text 在程序中設定文本的格式

   -------------------------------------------------------------------------------------

     &L  Left-aligns the characters that follow          左對齊

     &C  Centers the characters that follow             居中

     &R  Right-aligns the characters that follow         右對齊

     &E  Turns double-underline printing on or off      雙下划線

     &X  Turns superscript printing on or off            上標

     &Y  Turns subscript  Printing on or off             下標

     &B  Turns bold printing on or off                   粗體

     &I   Turns italic printing on or off                  斜體

     &U  Turns underline  printing on or off            下划線

     &S  Turns strikethrough(刪除線) printing on or off  刪除線

     &”fontname”

          指定文本的字體名字,確保字體名兩邊有雙引號

    &nn  用一個兩位數字指定字體的大小

Codes to insert specific data        在程序中插入指定的數據

      &D              prints the current date                當前日期

      &T              prints the current time                當前時間

      &F              prints the name of the document      文檔的名字

      &A          prints the name of the workbook tab (the “sheet name”)工作薄名

      &P              prints the page number                當前是第N頁

      &N              Prints the total numer of pages in the document總頁數

      &P+number     Prints the page number plus number   當前頁值+數字

      &P-number      prints the page number minus number  當前頁值-數字

      &&              Prints a single ampersand   &(=and)的記號名稱


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM