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)目中添加對(duì)這幾個(gè)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("編號(hào)");
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,可能會(huì)為空
//// 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
希望本文所述對(duì)大家的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# XML基礎(chǔ)入門小結(jié)(XML文件內(nèi)容增刪改查清)
本文主要介紹了C# XML基礎(chǔ)入門小結(jié)(XML文件內(nèi)容增刪改查清),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04c#實(shí)現(xiàn)無標(biāo)題欄窗口的拖動(dòng)
本篇文章是對(duì)c#中實(shí)現(xiàn)無標(biāo)題欄窗口拖動(dòng)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Unity3D 計(jì)時(shí)器的實(shí)現(xiàn)代碼(三種寫法總結(jié))
這篇文章主要介紹了Unity3D 計(jì)時(shí)器的實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04C#獲取鼠標(biāo)在listview右鍵點(diǎn)擊單元格的內(nèi)容方法
下面小編就為大家?guī)硪黄狢#獲取鼠標(biāo)在listview右鍵點(diǎn)擊單元格的內(nèi)容方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01C# Dictionary和SortedDictionary的簡(jiǎn)介
今天小編就為大家分享一篇關(guān)于C# Dictionary和SortedDictionary的簡(jiǎn)介,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10