C#WinFrom導(dǎo)出Excel過程解析
這篇文章主要介紹了C#WinFrom導(dǎo)出Excel過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
采用的是以DataGridView的形式導(dǎo)出,使用NPOI.dll
1.由于使用的是DataGridView,所以類需要創(chuàng)建在From的Project下,DLL導(dǎo)入NPOI


2.代碼如下
ExportExcel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NPOI.SS.UserModel; //NPOI
using NPOI.HSSF.Util; //NPOI
using NPOI.HSSF.UserModel; //NPOI
using NPOI.XSSF.UserModel; //NPOI
using System.IO;
namespace ESMT
{
public class ExportExcel
{
/// <summary>
///
/// </summary>
/// <param name="grdview">數(shù)據(jù)表</param>
/// <param name="sheetName">工作簿名字</param>
/// <param name="FilePath">文件路徑</param>
/// <param name="columnTitle">列頭</param>
public void ExportToExcel(DataGridView grdview, string sheetName, string FilePath, string[] columnTitle)
{
//不允許dataGridView顯示添加行,負責導(dǎo)出時會報最后一行未實例化錯誤
grdview.AllowUserToAddRows = false;
HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet(sheetName);//創(chuàng)建工作簿
//設(shè)置表頭
IRow headerRow = sheet.CreateRow(0);//創(chuàng)建第一行
headerRow.HeightInPoints = 40;
headerRow.CreateCell(0).SetCellValue("出庫表單");//單元格賦值
ICellStyle headStyle = workbook.CreateCellStyle();
headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;//格式居中
IFont font = workbook.CreateFont();
font.Boldweight = 500;
font.FontHeightInPoints = 20;
headStyle.SetFont(font);
headerRow.GetCell(0).CellStyle = headStyle;
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, grdview.ColumnCount - 2));//單元格合并 最后個參數(shù)是合并個數(shù)
IRow headerRow2 = sheet.CreateRow(1);//創(chuàng)建第二行列頭
ICellStyle headStyle2 = workbook.CreateCellStyle();
headStyle2.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
IFont font2 = workbook.CreateFont();
font2.FontHeightInPoints = 10;
font2.Boldweight = 700;
headStyle2.SetFont(font2);
for (int l = 0; l < grdview.ColumnCount - 1; l++) //列頭填值
{
headerRow2.CreateCell(l).SetCellValue(columnTitle[l]);
headerRow2.GetCell(l).CellStyle = headStyle2;
}
//設(shè)置列寬
for (int l = 0; l < grdview.Columns.Count; l++)
{
sheet.DefaultColumnWidth = 15;
}
//填寫內(nèi)容
for (int i = 0; i < grdview.Rows.Count; i++)
{
IRow row = sheet.CreateRow(i + 2);
for (int j = 1; j < grdview.Columns.Count; j++)
{
row.CreateCell(j - 1, CellType.String).SetCellValue(grdview.Rows[i].Cells[j].Value.ToString());//j-1表示哪個單元格
}
}
using (FileStream stream = File.OpenWrite(FilePath))//創(chuàng)建Excel并寫入數(shù)據(jù)
{
workbook.Write(stream);
stream.Close();
}
GC.Collect();
}
}
}
PS:openwtrie 打開或者創(chuàng)建新的文件寫入
3.From窗口點擊導(dǎo)出按鈕
導(dǎo)出按鈕
string[] columnTitle = { "序號", "倉位", "Facility", "供應(yīng)商料號", "料號", "料卷ID", "料卷數(shù)量", "儲位號", "Date Code/Lot", "生產(chǎn)日期", "供應(yīng)商編碼", "入倉時間" };
string localFilePath = "";// fileNameExt, newFileName, FilePath;
SaveFileDialog sfd = new SaveFileDialog();//保存文件窗口
//設(shè)置文件類型
sfd.Filter = "Excel(97-2003)|*.xls";//保存類型為EXCEL
//保存對話框是否記憶上次打開的目錄
sfd.RestoreDirectory = true;
//點了保存按鈕進入
if (sfd.ShowDialog() == DialogResult.OK)
{
localFilePath = sfd.FileName.ToString(); //獲得文件路徑
ex.ExportToExcel(grdData, "出庫表單", localFilePath, columnTitle);
}
通過以上三步,完成點擊導(dǎo)出按鈕,后選擇保存位置并命名,調(diào)用EportExcel方法完成導(dǎo)出Excel。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

