datagridview 匯出Excel檔案

2021-05-27 11:27:54 字數 778 閱讀 7654

/// /// 以datagridview 匯出excel檔案   

///

///

public void exportexcel(datagridview dgv)

microsoft.office.interop.excel.workbook workbook = workbooks.add(microsoft.office.interop.excel.xlwbatemplate.xlwbatworksheet);

microsoft.office.interop.excel.worksheet worksheet = (microsoft.office.interop.excel.worksheet)workbook.worksheets[1];//取得sheet1

microsoft.office.interop.excel.range range;

long totalcount = dgv.rows.count;

long rowread = 0;

float percent = 0;

//寫入字段

for (int i = 0; i < dgv.columns.count; i++)

//寫入數值

for (int r = 0; r < dgv.rows.count; r++)

else

}rowread++;

percent = ((float)(100 * rowread)) / totalcount;

}}

DataGridView匯出到Excel的三個方法

region datagridview資料顯示到excel 開啟excel並將datagridview控制項中資料匯出到excel datagridview物件 是否顯示excel介面 add com microsoft excel 11.0 object library using excel m...

從 DataGridView 匯出到 Excel

public sub exporttoexcel byval datagridview1 as datagridview if datagridview1.rows.count 1 then for i 0 to datagridview1.columncount 1 生成excel中列頭名稱 my...

DataGridView匯出資料到XML

using system using system.io using system.collections.generic using system.text using system.data using system.drawing using system.windows.forms name...