sample 1
1.在项目中打开Add 引用对话框,选择COM栏,之后在COM列表中找到
"Microsoft Excel 9.0 Object Library"(Office 2000)" "Microsoft Excel 11.0 Object Library"(Office 2003)"; 然后将其加入到项目的引用中即可。Visual C#.NET会自动产生相应的.NET组件文件,以后即可正常使用。
2.在代码中 加入:
using
System.IO;
using
System.Reflection;
using
Microsoft.Office.Interop.Excel;
3.从C#导出到Excel 常用命令:
//建立Excel
Excel.Application myExcel = new
Excel.Application ();
myExcel.Application.Workbooks.Add ( true
);
Excel.Workbook myBook =myExcel.Workbooks[1]; //工作薄
Excel.Worksheet mySheet =(Excel.Worksheet)myBook.ActiveSheet;//工作区
myExcel.Visible = true
;
//
合并单元格
//
mySheet.get_Range(mySheet.Cells[1,1],mySheet.Cells[9,9]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
// //Excel.Range myRange=mySheet.get_Range("A1","C1"); //
另一种方法
//
Excel.Range myRange=mySheet.get_Range(mySheet.Cells[1,1],mySheet.Cells[9,9]);
//
myRange.Merge(null);
//
myRange.Font.Bold =true;
//
myRange.NumberFormatLocal="¥#,##0.00;¥-#,##0.00";
//
myRange.set_Value(Excel.XlRangeValueDataType.xlRangeValueDefault,12345.6789);
//
行合并
//
mySheet.Cells[5, 1] = "制表人:余兴建";
//
mySheet.get_Range(mySheet.Cells[5,1],mySheet.Cells[5,5]).Select();
//
mySheet.get_Range(mySheet.Cells[5,1],mySheet.Cells[5,5]).Font.Bold = true;
//
mySheet.get_Range(mySheet.Cells[5,1],mySheet.Cells[5,5]).Font.Size = 10;
//
mySheet.get_Range(mySheet.Cells[5,1],mySheet.Cells[5,5]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
//设置单元格属性
myRange.NumberFormatLocal="¥#,##0.00;¥-#,##0.00"; //单元格输出类型
RangBt.RowHeight=30; //长
RangBt.ColumnWidth=36.5;//宽
RangBt.WrapText(); //换行
RangBt.ShrinkToFit=false;RangBt.MergeCells=false;//自适应高度
mySheet.get_Range(mySheet.Cells[rowStart + 1,colStart],mySheet.Cells[rowSum,intColSum]).Columns.AutoFit(); ////设置报表表格为最适应宽度
mySheet.get_Range(mySheet.Cells[rowIndex,colStart],mySheet.Cells[rowIndex,intColSum]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
**************************
绝招:先在Excel录制宏,再开看宏的代码。
//绘制边框
mySheet.get_Range(mySheet.Cells[10,1],mySheet.Cells[RowIndex,7]).Borders.LineStyle = 1
;
mySheet.get_Range(mySheet.Cells[10,1],mySheet.Cells[RowIndex,1]).Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThin;//xlThick;//设置左边线加粗
mySheet.get_Range(mySheet.Cells[10,1],mySheet.Cells[10,7]).Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThin;//设置上边线加粗
mySheet.get_Range(mySheet.Cells[10,7],mySheet.Cells[RowIndex,7]).Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThin;//设置右边线加粗
mySheet.get_Range(mySheet.Cells[RowIndex,1],mySheet.Cells[RowIndex,9]).Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThin;//
设置下边线加粗
//绘制一边
mySheet.get_Range(mySheet.Cells[3,1],mySheet.Cells[3,7]).Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight =
XlBorderWeight.xlThin;
//打印页面设置
mySheet.PageSetup.Orientation=Excel.XlPageOrientation.xlPortrait;//纵向 xlLandscape '横向
mySheet.PageSetup.PaperSize=Excel.XlPaperSize.xlPaperA4;//A4纸
string str="通过打印方向、打印纸张大小的设置,不断进行预览"
;
str+=((char)10).ToString()+"民生";//
换行
// mySheet.PageSetup.CenterHeader=str; //页眉
mySheet.PageSetup.CenterHorizontally=true; //
水平对齐
// mySheet.PageSetup.LeftFooter = "第 &P 页,共 &N 页"; //
设置居中页脚显示
//设置左右距离
mySheet.PageSetup.LeftMargin = 1.9/0.035; //设置左边距为1厘米
mySheet.PageSetup.RightMargin = 1.9/0.035;//设置右边距为1.5厘米
mySheet.PageSetup.TopMargin = 1.2/0.035;//设置上边距为2.2厘米
mySheet.PageSetup.BottomMargin = 2.3/0.035;//设置下边距为2.1厘米
sample 2
'
Excel設定値
Const
xlLandscape
=
2
'
用紙向き 横
Const
xlCenter
=
-
4108
'
中央揃え
Const
xlRight
=
-
4152
'
右揃え
Const
xlLeft
=
-
4131
'
左揃え
Const
xlInsideHorizontal
=
12
Const
xlContinuous
=
1
Const
xlThin
=
2
'
Excel列番号
Const
cnsCol1
As
Integer
=
1
Const
cnsCol2
As
Integer
=
2
Const
cnsCol3
As
Integer
=
3
'
Excel保存先
Const
FilePath
As
String
=
"
C:\Excel\Test.xls
"
'
Excelオブジェクト
Dim
oExcel
As
Object
'
Excel.Application
Dim
oBooks
As
Object
'
Excel.Workbooks
Dim
oBook
As
Object
'
Excel.Workbook
Dim
oSheets
As
Object
'
Excel.Sheets
Dim
oSheet
As
Object
'
Excel.Worksheet
Dim
oRange
As
Object
'
Excel.Range
Dim
oCells
As
Object
'
Excel.Range(1回の解放でOK)
Dim
oBorders
As
Object
'
Excel.Borders
Dim
oPageSetup
As
Object
'
Excel.PageSetup
Dim
GridData(,)
As
String
Dim
intRow
As
Integer
Dim
intRowCount
As
Integer
Try
'
Excelオブジェクトセット
oExcel
=
CreateObject
(
"
Excel.Application
"
)
oBooks
=
oExcel.Workbooks
oBook
=
oBooks.Add
oSheets
=
oBook.Worksheets
oSheet
=
oSheets(
1
)
oCells
=
oSheet.Cells
'
----- 1行目 -----
intRow
=
1
'
列幅設定
oRange
=
oCells(intRow, cnsCol1)
oRange.ColumnWidth
=
10.25
Call
MRComObject(oRange)
oRange
=
oCells(intRow, cnsCol2)
oRange.ColumnWidth
=
10.25
Call
MRComObject(oRange)
oRange
=
oCells(intRow, cnsCol3)
oRange.ColumnWidth
=
10.25
Call
MRComObject(oRange)
'
列タイトル 中央揃え
oRange
=
oSheet.Range(R1C1ToA1(intRow, cnsCol1, intRow, cnsCol3))
With
oRange
.HorizontalAlignment
=
xlCenter
.VerticalAlignment
=
xlCenter
End
With
Call
MRComObject(oRange)
'
列タイトル 表示
oCells(intRow, cnsCol1)
=
"
列名1
"
oCells(intRow, cnsCol2)
=
"
列名2
"
oCells(intRow, cnsCol3)
=
"
列名3
"
'
----- 2行目以降 -----
intRow
+=
1
'
データを表示する
ReDim
GridData(
1
,
2
)
GridData(
0
,
0
)
=
"
A1
"
: GridData(
0
,
1
)
=
"
B1
"
: GridData(
0
,
2
)
=
"
C1
"
GridData(
1
,
0
)
=
"
A2
"
: GridData(
1
,
1
)
=
"
B2
"
: GridData(
1
,
2
)
=
"
C2
"
oRange
=
oSheet.Range(R1C1ToA1(intRow, cnsCol1, intRow
+
1
, cnsCol3))
oRange.value
=
GridData
Call
MRComObject(oRange)
'
罫線を引く
oRange
=
oSheet.Range(R1C1ToA1(
1
, cnsCol1, GridDataNum
+
1
, cnsCol3))
oBorders
=
oRange.borders(xlInsideHorizontal)
'
横線
With
oBorders
oBorders.LineStyle
=
xlContinuous
oBorders.Weight
=
xlThin
End
With
Call
MRComObject(oBorders)
Call
MRComObject(oRange)
'
ページヘッダ部設定
oPageSetup
=
oSheet.PageSetup
With
oPageSetup
.LeftHeader
=
""
.CenterHeader
=
"
&20 入出庫履歴
"
.RightHeader
=
"
&10
"
&
"
日付 &D
"
&
"
現在
"
.Orientation
=
xlLandscape
.ZOOM
=
75
.LeftMargin
=
0
.RightMargin
=
0
End
With
Call
MRComObject(oPageSetup)
'
Excelを保存する
oExcel.DisplayAlerts
=
False
'
警告メッセージ非表示
oBook.SaveAs(FilePath)
'
Excel保存
oExcel.DisplayAlerts
=
True
'
警告メッセージ表示
Catch
ex
As
Exception
MsgBox
(Err.Description)
Finally
'
Excelオブジェクトの解放
Call
MRComObject(oCells)
'
oCells の解放
Call
MRComObject(oSheet)
'
oSheet の解放
Call
MRComObject(oSheets)
'
oSheets の解放
oBook.Close(
False
)
'
oBook を閉じる
Call
MRComObject(oBook)
'
oBook の解放
Call
MRComObject(oBooks)
'
oBooks の解放
oExcel.Quit()
'
oExcelを閉じる
Call
MRComObject(oExcel)
'
oExcel を解放
GC.Collect()
'
ガーベジコレクト起動
'
-----------------------------------------------------------
'
Excelプロセスチェック
System.Threading.Thread.Sleep(
1000
)
Dim
localByName
As
Process()
=
Process.GetProcessesByName(
"
Excel
"
)
If
localByName.Length
>
0
Then
MessageBox.Show(
"
まだ Excel.EXE が
"
&
localByName.Length
&
"
個 起動しています。
"
)
Else
MessageBox.Show(
"
起動している Excel.EXE はありません。
"
)
End
If
'
-----------------------------------------------------------
End
Try
'
***** R1C1形式のアドレスをA1形式に変換する *****
Private
Function
R1C1ToA1( _
ByVal
R1
As
Integer
, _
ByVal
C1
As
Integer
, _
ByVal
R2
As
Integer
, _
ByVal
C2
As
Integer
)
As
String
Try
Dim
i1, i2
As
Integer
Dim
d1, d2
As
String
i1
=
Math.Floor((C1
-
1
)
\
26
)
d1
=
IIf
(C1
>
26
, Strings.Chr(
64
+
i1),
""
)
i1
=
C1
-
26
*
i1
d1
&=
Strings.Chr(
64
+
i1)
&
CStr
(R1)
i2
=
Math.Floor((C2
-
1
)
\
26
)
d2
=
IIf
(C2
>
26
, Strings.Chr(
64
+
i2),
""
)
i2
=
C2
-
26
*
i2
d2
&=
Strings.Chr(
64
+
i2)
&
CStr
(R2)
Return
d1
&
"
:
"
&
d2
Catch
ex
As
Exception
MessageBox.Show(ex.ToString)
End
Try
End Function
'
***** COMオブジェクトへの参照を解放する *****
Private
Sub
MRComObject(
ByRef
ComObject
As
Object
)
Try
'
提供されたランタイム呼び出し可能ラッパーの参照カウントをデクリメントする
System.Runtime.InteropServices.Marshal.ReleaseComObject(ComObject)
Catch
Finally
'
参照を解除する
ComObject
=
Nothing
End
Try
End Sub
sample 3
Public
Function
ExportExcelLayout(
ByVal
filePath
As
String
,
ByVal
rowcount
As
Integer
,
ByVal
rrkFlg
As
Boolean
)
As
Boolean
Dim
flg
As
Boolean
Dim
xlsApp
As
Excel.Application
=
CreateObject
(
"
Excel.Application
"
)
xlsApp.Visible
=
False
Dim
xlsWkb
As
Excel.Workbook
=
xlsApp.Workbooks.Open(filePath)
Dim
sheet
As
Excel.Worksheet
Try
sheet
=
xlsApp.Worksheets(SHEETNAME)
'
セル色設定
sheet.Range(sheet.Cells(
1
,
1
), sheet.Cells(
1
,
90
)).Interior.Color
=
RGB
(
204
,
255
,
255
)
sheet.Range(
"
B1
"
).Interior.Color
=
RGB
(
255
,
255
,
153
)
sheet.Range(sheet.Cells(
1
,
91
), sheet.Cells(
1
,
171
)).Interior.Color
=
RGB
(
255
,
255
,
153
)
sheet.Range(
"
BJ1
"
).Interior.Color
=
RGB
(
192
,
192
,
192
)
sheet.Range(
"
BN1
"
).Interior.Color
=
RGB
(
192
,
192
,
192
)
sheet.Range(
"
BU1
"
).Interior.Color
=
RGB
(
192
,
192
,
192
)
sheet.Range(
"
CB1
"
).Interior.Color
=
RGB
(
192
,
192
,
192
)
sheet.Range(
"
CI1
"
).Interior.Color
=
RGB
(
192
,
192
,
192
)
'
セル罫線設定
sheet.Range(sheet.Cells(
1
,
1
), sheet.Cells(rowcount
+
1
,
171
)).Borders.Weight
=
2
sheet.Range(sheet.Cells(
1
,
1
), sheet.Cells(rowcount
+
1
,
171
)).Borders.LineStyle
=
1
'
折り返して全体を表示
sheet.Range(sheet.Cells(
2
,
1
), sheet.Cells(rowcount
+
1
,
171
)).WrapText
=
True
'
セルサイズ自動調整
sheet.Rows.AutoFit()
'
コメント挿入(内容とサイズ)
sheet.Range(
"
B1
"
).AddComment(
"
FIS区分""2""の案件が対象
"
)
sheet.Range(
"
C1
"
).AddComment(
"
対象勘定科目
"
&
Microsoft.VisualBasic.Constants.vbCrLf
&
_
"
・コンピュータリース料(FIS含む)
"
&
Microsoft.VisualBasic.Constants.vbCrLf
&
_
"
・コンピュータレンタル料(FIS含む)
"
&
Microsoft.VisualBasic.Constants.vbCrLf
&
_
"
・コンピュータ保守料(FIS含む)
"
&
Microsoft.VisualBasic.Constants.vbCrLf
&
_
"
・オペレータ委託料(FIS含む)
"
)
sheet.Range(
"
C1
"
).Comment.Shape.Height
=
120
'
計算式設定
For
i
As
Integer
=
2
To
rowcount
+
1
If
Not
sheet.Cells(i,
2
).Text.Equals(
String
.Empty)
Then
sheet.Range(sheet.Cells(i,
3
), sheet.Cells(i,
46
)).Formula
=
"
=IF((R[-1]C=""""), """", R[-1]C)
"
ElseIf
Not
rrkFlg
Then
sheet.Range(
"
BK
"
&
i).Formula
=
"
=RC[-16]-RC[-1]
"
sheet.Range(
"
BO
"
&
i).Formula
=
"
=RC[-20]-RC[-1]
"
sheet.Range(
"
BR
"
&
i).Formula
=
"
=RC[-21]-RC[-23]
"
sheet.Range(
"
BV
"
&
i).Formula
=
"
=RC[-25]-RC[-1]
"
sheet.Range(
"
BY
"
&
i).Formula
=
"
=RC[-22]-RC[-28]
"
sheet.Range(
"
CC
"
&
i).Formula
=
"
=RC[-26]-RC[-1]
"
sheet.Range(
"
CF
"
&
i).Formula
=
"
=RC[-25]-RC[-29]
"
sheet.Range(
"
CJ
"
&
i).Formula
=
"
=RC[-29]-RC[-1]
"
End
If
Next
'
自動フィルタリング
sheet.Range(
"
B1
"
).AutoFilter(
1
, VisibleDropDown:
=
True
)
'
ページ設定
sheet.PageSetup.Orientation
=
XlPageOrientation.xlLandscape
sheet.PageSetup.BlackAndWhite
=
False
sheet.PageSetup.PrintGridlines
=
False
sheet.PageSetup.PrintHeadings
=
False
sheet.PageSetup.PrintTitleRows
=
"
$1:$1
"
sheet.PageSetup.Zoom
=
65
sheet.PageSetup.PaperSize
=
XlPaperSize.xlPaperA4
sheet.PageSetup.BottomMargin
=
xlsApp.CentimetersToPoints(
1.7
)
sheet.PageSetup.RightMargin
=
xlsApp.CentimetersToPoints(
0.5
)
sheet.PageSetup.LeftMargin
=
xlsApp.CentimetersToPoints(
0.5
)
sheet.PageSetup.TopMargin
=
xlsApp.CentimetersToPoints(
2
)
sheet.PageSetup.HeaderMargin
=
xlsApp.CentimetersToPoints(
1.6
)
sheet.PageSetup.FooterMargin
=
xlsApp.CentimetersToPoints(
1.3
)
'
sheet.PageSetup.FooterMargin = xlsApp.InchesToPoints(1.3)
sheet.PageSetup.CenterHorizontally
=
True
sheet.PageSetup.RightHeader
=
"
&D
"
sheet.PageSetup.CenterFooter
=
"
&P / &N
"
'
自動計算(計算式に関する)
xlsApp.CalculateBeforeSave
=
False
xlsApp.Calculation
=
XlCalculation.xlCalculationAutomatic
xlsApp.CalculateFull()
'
ズーム
xlsApp.ActiveWindow.Zoom
=
75
'
シート保護
sheet.Protect(Contents:
=
True
, _
UserInterfaceOnly:
=
sheet.ProtectionMode, _
AllowFormattingCells:
=
sheet.Protection.AllowFormattingCells, _
AllowFormattingColumns:
=
True
, _
AllowFormattingRows:
=
sheet.Protection.AllowFormattingRows, _
AllowInsertingColumns:
=
sheet.Protection.AllowInsertingColumns, _
AllowInsertingRows:
=
sheet.Protection.AllowInsertingRows, _
AllowInsertingHyperlinks:
=
sheet.Protection.AllowInsertingHyperlinks, _
AllowDeletingColumns:
=
sheet.Protection.AllowDeletingColumns, _
AllowDeletingRows:
=
sheet.Protection.AllowDeletingRows, _
AllowSorting:
=
sheet.Protection.AllowSorting, _
AllowFiltering:
=
True
, _
AllowUsingPivotTables:
=
sheet.Protection.AllowUsingPivotTables)
xlsWkb.Save()
flg
=
True
Catch
ex
As
Exception
flg
=
False
Finally
xlsWkb.Close()
xlsApp.Quit()
FinalReleaseComObject(sheet)
FinalReleaseComObject(xlsWkb)
FinalReleaseComObject(xlsApp)
End
Try
Return
flg
End Function
【Excel】色一覧
Excelにおいて標準で選択できる色のColorIndex・色名と、16進表記・HTMLカラーネームの一覧です。
HTMLカラーネームが付いているのは16色だけか……Webセーフカラーじゃないものもあるし
Color Index |
色見本 |
色名 |
16進表記 |
HTML カラー ネーム |
0 |
(自動) |
自動 |
(void) |
(void) |
1 |
|
黒 |
#000000 |
black |
2 |
|
白 |
#FFFFFF |
white |
3 |
|
赤 |
#FF0000 |
red |
4 |
|
明るい緑 |
#00FF00 |
lime |
5 |
|
青 |
#0000FF |
blue |
6 |
|
黄 |
#FFFF00 |
yellow |
7 |
|
ピンク |
#FF00FF |
fuchsia |
8 |
|
水色 |
#00FFFF |
aqua |
9 |
|
濃い赤 |
#800000 |
maroon |
10 |
|
緑 |
#008000 |
green |
11 |
|
濃い青 |
#000080 |
navy |
12 |
|
濃い黄 |
#808000 |
olive |
13 |
|
紫 |
#800080 |
purple |
14 |
|
青緑 |
#008080 |
teal |
15 |
|
25%灰色 |
#C0C0C0 |
silver |
16 |
|
50%灰色 |
#808080 |
gray |
17 |
|
グレー |
#9999FF |
(void) |
18 |
|
プラム |
#993366 |
(void) |
19 |
|
アイボリー |
#FFFFCC |
(void) |
20 |
|
薄い水色 |
#CCFFFF |
(void) |
21 |
|
濃い紫 |
#660066 |
(void) |
22 |
|
コーラル |
#FF8080 |
(void) |
23 |
|
オーシャンブルー |
#0066CC |
(void) |
24 |
|
アイスブルー |
#CCCCFF |
(void) |
25 |
|
濃い青 |
#000080 |
navy |
26 |
|
ピンク |
#FF00FF |
fuchsia |
27 |
|
黄 |
#FFFF00 |
yellow |
28 |
|
水色 |
#00FFFF |
aqua |
29 |
|
紫 |
#800080 |
purple |
30 |
|
濃い赤 |
#800000 |
maroon |
31 |
|
青緑 |
#008080 |
teal |
32 |
|
青 |
#0000FF |
blue |
33 |
|
スカイブルー |
#00CCFF |
(void) |
34 |
|
薄い水色 |
#CCFFFF |
(void) |
35 |
|
薄い緑 |
#CCFFCC |
(void) |
36 |
|
薄い黄 |
#FFFF99 |
(void) |
37 |
|
ペールブルー |
#99CCFF |
(void) |
38 |
|
ローズ |
#FF99CC |
(void) |
39 |
|
ラベンダー |
#CC99FF |
(void) |
40 |
|
ベージュ |
#FFCC99 |
(void) |
41 |
|
薄い青 |
#3366FF |
(void) |
42 |
|
アクア |
#33CCCC |
(void) |
43 |
|
ライム |
#99CC00 |
(void) |
44 |
|
ゴールド |
#FFCC00 |
(void) |
45 |
|
薄いオレンジ |
#FF9900 |
(void) |
46 |
|
オレンジ |
#FF6600 |
(void) |
47 |
|
ブルーグレー |
#666699 |
(void) |
48 |
|
40%灰色 |
#969696 |
(void) |
49 |
|
濃い青緑 |
#003366 |
(void) |
50 |
|
シーグリーン |
#339966 |
(void) |
51 |
|
濃い緑 |
#003300 |
(void) |
52 |
|
オリーブ |
#333300 |
(void) |
53 |
|
茶 |
#993300 |
(void) |
54 |
|
プラム |
#993366 |
(void) |
55 |
|
インディゴ |
#333399 |
(void) |
56 |
|
80%灰色 |
#333333 |
(void) |