c# 讀取寫入excel儲存格(包括對excel的一些基本操作)以下是一些對excel的一些基本操作

來源:互聯網
上載者:User
c# 讀取寫入excel儲存格(包括對excel的一些基本操作)

以下是一些對excel的一些基本操作

1:工程對excel類庫的匯入,如:c:\program files\Microsoft office\offiece11\excel.exe
2:命名控制項的引入: using Microsoft.office.Interop.Excel;

3:如果是對一個已經存在的excel檔案進行操作則:
Application app=new Application();
Workbook wbook=app.Workbooks.Open("c:\\temp.xls",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);

Worksheet worksheet=(Worksheet)wbook.Worksheets[1];

4:如果是建立一個excel檔案:
Application app=new Application();
Workbook wbook=app.Workbook.Add(Type.missing);
Worksheet worksheet=(Worksheet)wbook.Worksheets[1];

5:設定某個儲存格裡的內容:
worksheet.Cells[1,2]="列內容"

6讀取某個儲存格裡的內容
string temp=((Range)worksheet.Cells[1,2]).Text;

7設定某個儲存格裡的格式
Excel.Range rtemp=worksheet.get_Range("A1","A1");
rtemp.Font.Name="宋體";
rtemp.Font.FontStyle="加粗";
rtemp.Font.Size=5;

8 儲存建立的內容:
worksheet.SaveAs("c:\\temp.xls",Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing);

 

來自:http://www.cnblogs.com/farrell/archive/2008/01/24/1051273.html

range.NumberFormatLocal = "@";     //設定儲存格格式為文本

range = (Range)worksheet.get_Range("A1", "E1");     //擷取Excel多個儲存格範圍:本例做為Excel表頭

range.Merge(0);     //儲存格合并動作

worksheet.Cells[1, 1] = "Excel儲存格賦值";     //Excel儲存格賦值

range.Font.Size = 15;     //設定字型大小

range.Font.Underline=true;     //設定字型是否有底線

range.Font.Name="黑體";      設定字型的種類

range.HorizontalAlignment=XlHAlign.xlHAlignCenter;     //設定字型在儲存格內的對其方式

range.ColumnWidth=15;     //設定儲存格的寬度

range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb();     //設定儲存格的背景色

range.Borders.LineStyle=1;     //設定儲存格邊框的粗細

range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb());     //給儲存格加邊框

range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone; //設定儲存格上邊框為無邊框

range.EntireColumn.AutoFit();     //自動調整列寬

Range.HorizontalAlignment= xlCenter;     // 文本水平置中方式

Range.VerticalAlignment= xlCenter     //文本垂直置中方式

Range.WrapText=true;     //文本自動換行

Range.Interior.ColorIndex=39;     //填充顏色為淡紫色

Range.Font.Color=clBlue;     //字型顏色

xlsApp.DisplayAlerts=false;     //儲存Excel的時候,不彈出是否儲存的視窗直接進行儲存

=======================

獲得range.MergeArea.Rows.Count合并的範圍

 

以下是一些對excel的一些基本操作

1:工程對excel類庫的匯入,如:c:\program files\Microsoft office\offiece11\excel.exe
2:命名控制項的引入: using Microsoft.office.Interop.Excel;

3:如果是對一個已經存在的excel檔案進行操作則:
Application app=new Application();
Workbook wbook=app.Workbooks.Open("c:\\temp.xls",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);

Worksheet worksheet=(Worksheet)wbook.Worksheets[1];

4:如果是建立一個excel檔案:
Application app=new Application();
Workbook wbook=app.Workbook.Add(Type.missing);
Worksheet worksheet=(Worksheet)wbook.Worksheets[1];

5:設定某個儲存格裡的內容:
worksheet.Cells[1,2]="列內容"

6讀取某個儲存格裡的內容
string temp=((Range)worksheet.Cells[1,2]).Text;

7設定某個儲存格裡的格式
Excel.Range rtemp=worksheet.get_Range("A1","A1");
rtemp.Font.Name="宋體";
rtemp.Font.FontStyle="加粗";
rtemp.Font.Size=5;

8 儲存建立的內容:
worksheet.SaveAs("c:\\temp.xls",Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing,Type.missing);

 

來自:http://www.cnblogs.com/farrell/archive/2008/01/24/1051273.html

range.NumberFormatLocal = "@";     //設定儲存格格式為文本

range = (Range)worksheet.get_Range("A1", "E1");     //擷取Excel多個儲存格範圍:本例做為Excel表頭

range.Merge(0);     //儲存格合并動作

worksheet.Cells[1, 1] = "Excel儲存格賦值";     //Excel儲存格賦值

range.Font.Size = 15;     //設定字型大小

range.Font.Underline=true;     //設定字型是否有底線

range.Font.Name="黑體";      設定字型的種類

range.HorizontalAlignment=XlHAlign.xlHAlignCenter;     //設定字型在儲存格內的對其方式

range.ColumnWidth=15;     //設定儲存格的寬度

range.Cells.Interior.Color=System.Drawing.Color.FromArgb(255,204,153).ToArgb();     //設定儲存格的背景色

range.Borders.LineStyle=1;     //設定儲存格邊框的粗細

range.BorderAround(XlLineStyle.xlContinuous,XlBorderWeight.xlThick,XlColorIndex.xlColorIndexAutomatic,System.Drawing.Color.Black.ToArgb());     //給儲存格加邊框

range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone; //設定儲存格上邊框為無邊框

range.EntireColumn.AutoFit();     //自動調整列寬

Range.HorizontalAlignment= xlCenter;     // 文本水平置中方式

Range.VerticalAlignment= xlCenter     //文本垂直置中方式

Range.WrapText=true;     //文本自動換行

Range.Interior.ColorIndex=39;     //填充顏色為淡紫色

Range.Font.Color=clBlue;     //字型顏色

xlsApp.DisplayAlerts=false;     //儲存Excel的時候,不彈出是否儲存的視窗直接進行儲存

=======================

獲得range.MergeArea.Rows.Count合并的範圍

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.