winform導(dǎo)出dataviewgrid數(shù)據(jù)為excel的方法
本文實(shí)例講述了winform導(dǎo)出dataviewgrid數(shù)據(jù)為excel的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
private void btnExportList_Click(object sender, EventArgs e)
{
string fname = string.Empty;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "表格文件|*.xls";
sfd.DefaultExt = "xls";
if (sfd.ShowDialog() == DialogResult.OK)
{
fname = sfd.FileName;
}
else
{
return;
}
//導(dǎo)出當(dāng)前dataGridView中的所有數(shù)據(jù)到xls文件
//1.引入庫文件,新建lib文件夾,復(fù)制相關(guān)文件
//2.在項(xiàng)目中添加對這幾個dll的引用
//3.在內(nèi)存中建立 excel表文件
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet("第一頁");
//創(chuàng)建標(biāo)題頭
HSSFRow title = sheet.CreateRow(0);
title.CreateCell(0).SetCellValue("編號");
title.CreateCell(1).SetCellValue("姓名");
title.CreateCell(2).SetCellValue("性別");
title.CreateCell(3).SetCellValue("年齡");
title.CreateCell(4).SetCellValue("地址");
title.CreateCell(5).SetCellValue("電話");
title.CreateCell(6).SetCellValue("生日");
for (int rowindex = 0; rowindex < dgvStudens.RowCount; rowindex++)
{
//創(chuàng)建第一行
HSSFRow row = sheet.CreateRow(rowindex + 1);
for (int colindex = 0; colindex < dgvStudens.Rows[rowindex].Cells.Count; colindex++)
{
row.CreateCell(colindex).SetCellValue((dgvStudens.Rows[rowindex].Cells[colindex].Value == null) ? null : dgvStudens.Rows[rowindex].Cells[colindex].Value.ToString());
}
////創(chuàng)建第一行的第一列
//HSSFCell cell = row.CreateCell(0);
//cell.SetCellType(3);
//cell.SetCellValue(dgvStudens.Rows[rowindex].Cells[0].Value.ToString());
////第一行第2列
//row.CreateCell(1).SetCellValue(dgvStudens.Rows[rowindex].Cells[1].Value.ToString());
////第一行第3列
//row.CreateCell(2).SetCellValue(dgvStudens.Rows[rowindex].Cells[2].Value.ToString());
////第一行第4列,age,可能會為空
//// row.CreateCell(3).SetCellValue(dgvStudens.Rows[0].Cells[3].Value.ToString());
//row.CreateCell(3).SetCellValue((dgvStudens.Rows[rowindex].Cells[3].Value == null) ? null : dgvStudens.Rows[rowindex].Cells[3].Value.ToString());
}
using (FileStream fs = new FileStream(fname, FileMode.Create))
{
workbook.Write(fs);
}
;
}
#endregion
希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。
- winform中的ListBox和ComboBox綁定數(shù)據(jù)用法實(shí)例
- WinForm實(shí)現(xiàn)為ComboBox綁定數(shù)據(jù)源并提供下拉提示功能
- C#(WinForm) ComboBox和ListBox添加項(xiàng)及設(shè)置默認(rèn)選擇項(xiàng)
- 綁定winform中DataGrid
- C#在winform中實(shí)現(xiàn)數(shù)據(jù)增刪改查等功能
- Winform實(shí)現(xiàn)調(diào)用asp.net數(shù)據(jù)接口實(shí)例
- .Net中導(dǎo)出數(shù)據(jù)到Excel(asp.net和winform程序中)
- C#數(shù)據(jù)導(dǎo)入/導(dǎo)出Excel文件及winForm導(dǎo)出Execl總結(jié)
- WinForm中窗體間的數(shù)據(jù)傳遞交互的一些方法
- WinForm中comboBox控件數(shù)據(jù)綁定實(shí)現(xiàn)方法
相關(guān)文章
深入理解c# checked unchecked 關(guān)鍵字
本篇文章是對c#中的checked unchecked 關(guān)鍵字進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Unity3D實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲(2)
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲的第二部分,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
C#開發(fā)Windows服務(wù)實(shí)例之實(shí)現(xiàn)禁止QQ運(yùn)行
這篇文章主要介紹了通過C#開發(fā)Windows服務(wù),查殺qq進(jìn)程的服務(wù)功能,需要的朋友可以參考下2013-10-10
c#標(biāo)準(zhǔn)idispose模式使用示例
下面將把C#里實(shí)現(xiàn)IDispose模式的代碼展現(xiàn)出來,大家一起來學(xué)習(xí)一下,它的使用場合也很多的,當(dāng)我們手動對網(wǎng)站,數(shù)據(jù)庫作封裝時,都會用的到2014-02-02

