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文件夾,復制相關文件
//2.在項目中添加對這幾個dll的引用
//3.在內存中建立 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#程序設計有所幫助。
- winform中的ListBox和ComboBox綁定數(shù)據(jù)用法實例
- WinForm實現(xiàn)為ComboBox綁定數(shù)據(jù)源并提供下拉提示功能
- C#(WinForm) ComboBox和ListBox添加項及設置默認選擇項
- 綁定winform中DataGrid
- C#在winform中實現(xiàn)數(shù)據(jù)增刪改查等功能
- Winform實現(xiàn)調用asp.net數(shù)據(jù)接口實例
- .Net中導出數(shù)據(jù)到Excel(asp.net和winform程序中)
- C#數(shù)據(jù)導入/導出Excel文件及winForm導出Execl總結
- WinForm中窗體間的數(shù)據(jù)傳遞交互的一些方法
- WinForm中comboBox控件數(shù)據(jù)綁定實現(xiàn)方法
相關文章
Unity3D實現(xiàn)飛機大戰(zhàn)游戲(2)
這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)飛機大戰(zhàn)游戲的第二部分,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06
C#開發(fā)Windows服務實例之實現(xiàn)禁止QQ運行
這篇文章主要介紹了通過C#開發(fā)Windows服務,查殺qq進程的服務功能,需要的朋友可以參考下2013-10-10

