程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# Excel 操作個人小結

C# Excel 操作個人小結

編輯:C#入門知識

把整個Excel當做數據庫讀入的方式

string ExcelConnection = "Provider=Microsoft.Jet.Oledb.4.0;Data Source={0};Extended Properties=Excel 8.0";

string ExcelFileConnection = string.Format(ExcelConnection,FileName);

 

string queryString = string.Format("select * from [{0}$]", sheetName);

OleDbConnection oledbConn = new OleDbConnection(connString);

OleDbDataAdapter oledbAdap = new OleDbDataAdapter(queryString, oledbConn);

DataSet  dsResult = new DataSet();

oledbAdap.Fill(dsResult, fileName);

 

把整個Excel當做Com對象讀入的方式

打開Excel

Microsoft.Office.Interop.Excel.Application appExcel = new Microsoft.Office.Interop.Excel.Application();

Workbook wbkExcel = appExcel.Workbooks.Open(fileName
                                                          , Missing.Value
                                                          , Missing.Value
                                                          , Missing.Value
                                                          , Missing.Value
                                                          , Missing.Value
                                                          , Missing.Value
                                                          , Missing.Value
                                                          , Missing.Value
                                                          , Missing.Value
                                                          , Missing.Value
                                                          , Missing.Value
                                                          , Missing.Value
                                                          , Missing.Value
                                                          , Missing.Value);

關閉Excel

wbkExcel .Close(false, fileName, false);

另存為Excel

wbkExcel .SaveCopyAs(savePath.FileName);

取得sheet對象

Worksheet wstExcel = wbkExcel .Sheets[sheetName];

取得一個Cell

public static Range GetOneCell(Worksheet wst,int ColumnIndex, int RowIndex) 
{
            Range cell = (Range)wst.Cells[RowIndex, ColumnIndex];
            return cell;
}

取得一行

public static Range GetOneRowCells(Worksheet wst, int RowIndex) 
{
            Range row = wst.get_Range(wst.Cells[RowIndex, 1], wst.Cells[RowIndex, wst.Columns.Count]);
            return row;
}

取得一列

 public static Range GetOneColumnCells(Worksheet wst, int ColumnIndex)
 {
            Range column = wst.get_Range(wst.Cells[1, ColumnIndex], wst.Cells[wst.Rows.Count, ColumnIndex]);
            return column;
 }

 

同一本workbook的不同sheet的copy

//模板文件

 Workbook wbkTemplate = T_EXECLE.GetWorkBook(this.TemplateFullName);

//被copy的sheet拷貝到自己的後面

 wsData.Copy(wsData, Missing.Value);

//取得copy後的sheet
 Worksheet newSheet = (Worksheet)wbkTemplate.ActiveSheet;

//重命sheet名
 newSheet.Name = InstanceSheetNames[index];

 

行的copy(帶格式)

//模板行的取得

Range TemplateRow = T_EXECLE.GetOneRowCells(classSheet, StartIndex + 1);

//插入一個copy行

 //1 Select Template Row
TemplateRow.Select();
//2 Insert Blank Row
TemplateRow.Ent

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved