winform導出dataviewgrid數(shù)據(jù)為excel的方法
本文實例講述了winform導出dataviewgrid數(shù)據(jù)為excel的方法。分享給大家供大家參考。具體實現(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;
}
//導出當前dataGridView中的所有數(shù)據(jù)到xls文件
//1.引入庫文件,新建lib文件夾,復制相關(guān)文件
//2.在項目中添加對這幾個dll的引用
//3.在內(nèi)存中建立 excel表文件
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet("第一頁");
//創(chuàng)建標題頭
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è)計有所幫助。
- winform中的ListBox和ComboBox綁定數(shù)據(jù)用法實例
- WinForm實現(xiàn)為ComboBox綁定數(shù)據(jù)源并提供下拉提示功能
- C#(WinForm) ComboBox和ListBox添加項及設(shè)置默認選擇項
- 綁定winform中DataGrid
- C#在winform中實現(xiàn)數(shù)據(jù)增刪改查等功能
- Winform實現(xiàn)調(diào)用asp.net數(shù)據(jù)接口實例
- .Net中導出數(shù)據(jù)到Excel(asp.net和winform程序中)
- C#數(shù)據(jù)導入/導出Excel文件及winForm導出Execl總結(jié)
- WinForm中窗體間的數(shù)據(jù)傳遞交互的一些方法
- WinForm中comboBox控件數(shù)據(jù)綁定實現(xiàn)方法
相關(guān)文章
C# XML基礎(chǔ)入門小結(jié)(XML文件內(nèi)容增刪改查清)
本文主要介紹了C# XML基礎(chǔ)入門小結(jié)(XML文件內(nèi)容增刪改查清),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04Unity3D 計時器的實現(xiàn)代碼(三種寫法總結(jié))
這篇文章主要介紹了Unity3D 計時器的實現(xiàn)代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04C#獲取鼠標在listview右鍵點擊單元格的內(nèi)容方法
下面小編就為大家?guī)硪黄狢#獲取鼠標在listview右鍵點擊單元格的內(nèi)容方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01C# Dictionary和SortedDictionary的簡介
今天小編就為大家分享一篇關(guān)于C# Dictionary和SortedDictionary的簡介,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10