C 使用winform簡單匯出Excel的方法

2022-09-26 05:51:11 字數 1829 閱讀 7559

using excel;

在專案中引入excel.dll

///

/// 匯出excel

///

///

///

private void btnexportexcel_click(object sender, eventargs e)

if (dt.rows.count == 0)

excel.application xlapp = new excel.application();

if (xlapp == null)

xlapp.usercontrol = true;

excel.workbooks workbooks = xlapp.workbooks;

//根據模版產生新的workbook //workbook workbook = workbooks.add("d:\\aa.xls");

excel.workbook workbook = workbooks.add(excel.xlwbatemplate.xlwbatworksheet);

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

if (worksheet == null)

try//寫入數值

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

ppilwn rowread++;

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

//如果字的數量過多則自動換行。worksheet.cells[r+1, 4]為worksheet.cells[行, 列]

worksheet.get_range(worksheet.cells[r + 3, 4], worksheet.cells[r + 1, 4]).columns.wraptext = true; //自動換行

worksheet.get_range(worksheet.cells[r + 3, 4], worksheet.cells[r + 3, 4]).rows.autofit(); //自動加行高

= "匯出資料[" + 程式設計客棧percent.tostring("0.00") + "%]...";

}range = worksheet.get_range(worksheet.cells[2, 1], worksheet.cells[dt.rows.count + 2, dt.columns.count]);

range.borderaround(excel.xllinestyle.xlcontinuous, excel.xlborderweight.xlthin, excel.xlcolorindex.xlcolorindexautomatic, null);

range.borders[excel.xlbordersindex.xlinsidehorizontal].colorindex = excel.xlcolorindex.xlcolorindexautomatic;

range.borders[excel.xlbordersindex.xlinsidehorizontal].linestyle = excel.xllinestyle.xlcontinuous;

range.borders[excel.xlbordersindex.xlinsidehorizontal].weight = excel.xlborderweight.xlthin;

if (dt.columns.count > 1)

xlapp.visible = true;

} catch

finally

}

poi使用HSSFWorkbook 匯出excel

hssfworkbook 為office2003 xssfworkbook 為office2007以上可用 poi org.junit.test public void poi1test 合併單元格 合併日期佔兩行 4個引數,分別為起始行,結束行,起始列,結束列 行和列都是從0開始計數,且起始結束都...

WinForm實現簡單的拖拽功能 C

用到了listbox和treeview兩個控制項,listbox作為資料來源,通過拖拽其中的資料放置到treeview上,自動新增乙個樹節點 listbox控制項的mousedown用於獲取要拖拽的值並呼叫dodragdrop方法 private void listbox1 mousedown ob...

GridView使用自定義表頭和匯出到Excel

如上圖 檢視列 是靜態新增上的,表頭和資料則是動態新增的。如下 using system using system.data using system.configuration using system.collections using system.web using system.web.s...