C 開發Excel報表二

2021-04-28 13:00:16 字數 1736 閱讀 4952

在這裡介紹一下excel開發報表用到的一些基本的方法,例如單元格的合併,行與列的插入等操作。以及在excel中遍歷出相鄰單元格的並合併的方法。

單元格合併,單元格合併需要注意的是在合併之前需要清空帶合併單元格的資料,否則在合併時會彈出提示框,在合併之後在給單元格賦值。

//合併單元格

public void rangemerge(string pos1, string pos2,string name,object value)

catch (exception ex)

}向excel中插入一行

//在excel中插入空白行

public void insertrow(int index)

向excel中插入一列

//在excel中插入空白列

public void insertcolumns(int index)

接下來遍歷同一行中的資料相同的相鄰列,並執行合併操作

///

/// //合併同一行中資料相同的連續列

///

/// 比較的資料

/// 當前行的索引

/// 帶比較列的索引

/// 進行比較的範圍,即最後乙個待比較列的索引

/// 待合併範圍的第一列的索引

private void mergercolumn(string s1, int rowindex, int currentindex, int endindex, int columnindex)

else

//獲取當前待比較單元格是資料

string s2 = excelfilehandle.getrange(index + rowindex.tostring(), true).value2 == null ? "startcloumn" : excelfilehandle.getrange(index + rowindex.tostring().tostring(), true).value2.tostring();

//當待比較資料與比較資料相同時,則繼續比較該行的下一列的資料

if (s1 == s2)

else //當待比較資料與比較資料不同時,要考慮將之前相同列的資料進行合併

else

//獲取相同列中最後一列的索引號

string endindex ;

if (currentindex - 1 > convertcolumnidtoint('z'))

else

//如果存在兩個或者兩個以上連續的相同列,則合併

if (columnindex < currentindex - 1)

//對該行的下一列資料的進行比較合併

//設定下一列的索引

string nextindex;

if (currentindex > convertcolumnidtoint('z'))

else

s1 = excelfilehandle.getrange(nextindex + rowindex.tostring(), true).value2 == null ? "lastcloumn" : excelfilehandle.getrange(nextindex + rowindex.tostring().tostring(), true).value2.tostring();

mergercolumn(s1, rowindex, currentindex + 1, endindex, currentindex);

columnindex++;}}

}

C 開發EXCEL報表

最近專案中需要做匯出excel以及具有複雜表頭excel的彙總,其實都不算麻煩,基本上都做過匯出excel,具有複雜表頭excel的彙總以前也用active控制項實現了,但是效率都不是太理想。最後在乙個群友的提醒下,嘗試著用opendatasource datarowset解決,結果發現效率還不錯。...

C 開發Excel報表一

最近做了很長時間的excel開發,雖說有些報表的要求很 但是成果與時間還是成反比,尤其是報表的顯示太依賴與資料的結構,一旦系統中的資料結構不符合要求時,總是會導致程式出現錯誤。雖然程式最終通過了測試,但是在發布之後的第一天還是出現了諸多問題,總結下來有三類問題,一是型別轉換時出現溢位的現象,二是執行...

C 操作excel(報表)

新增乙個worksheet worksheet worksheet worksheet workbook.worksheets.add system.type.missing,system.type.missing,system.type.missing,system.type.missing ro...