国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# Excel 操作個人小結

2023-05-18 12:33:17
字體:
來源:轉載
供稿:網友

把整個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.EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
//3 Select NewRow
Range newRange = TemplateRow.Application.ActiveCell;
//4 Paste Data

TemplateRow.Copy(newRange);

 Range的copy(帶格式)

//要copy的數據

 Range dataRange = dataSheet.get_Range(dataSheet.Cells[StartIndex, "B"], InstanceSheet.Cells[EndIndex, "K"]);

//copy到那個sheet的Range內
 Range copyWhere = TemplateSheet.get_Range(TemplateSheet.Cells[T_StartIndex, "B"], TemplateSheet.Cells[T_StartIndex + I_Count, "R"]);

//copy執行
 dataRange.Copy(copyWhere);

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 盐山县| 北辰区| 栖霞市| 伽师县| 潜山县| 和硕县| 广宁县| 梨树县| 辰溪县| 磴口县| 封丘县| 绥阳县| 治多县| 新宁县| 达州市| 酉阳| 中卫市| 陇川县| 田东县| 会昌县| 阳城县| 民勤县| 镇平县| 无棣县| 莎车县| 广灵县| 崇义县| 盐边县| 多伦县| 寻甸| 神池县| 科尔| 崇信县| 安顺市| 盐池县| 松江区| 金秀| 外汇| 咸阳市| 额济纳旗| 银川市|