C#純技術——導出Excel

引言:Excel表格數據是經常出現在工程領域的數據分析中,導出Excel純C#代碼方法

開始:

1.添加引用

NPOI.dll

using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;

2.創建DataTable 方法 

 public static DataTable CreateTable()
        {
            string[] FiveHeader = { "ID", "班級", "職務", "姓名", "學號" };
            DataTable dataTable = new DataTable("ExportData");//*新建一張表
            foreach (string TemStr in FiveHeader)
            {
                DataColumn strNameColumn = new DataColumn(); 
                strNameColumn.DataType = typeof(String);     
                strNameColumn.ColumnName = TemStr;
                dataTable.Columns.Add(strNameColumn);       //*建立五列數據
            }
            for (int i = 0; i < 10; i++)
            {
                DataRow rowData = dataTable.NewRow();   //*建立行數據
                rowData["ID"] = i.ToString();
                rowData["班級"] = "一班";
                rowData["職務"] = "學生";
                rowData["姓名"] = "豬小明";
                rowData["學號"] = "2333-3333";
                dataTable.Rows.Add(rowData);

            }
            return dataTable;
        }

3.導出DataTable即ExportExcel

 public static bool DataTableToExcel(DataTable dataTable)
        {
            bool result = false;
            IWorkbook workbook = null;
            FileStream fs = null;
            IRow row = null;
            ISheet sheet = null;
            ICell cell = null;
            try
            {
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    workbook = new HSSFWorkbook();
                    sheet = workbook.CreateSheet("Sheet1");
                    int rowCount = dataTable.Rows.Count;
                    int columnCount = dataTable.Columns.Count;

                    //設置列頭  
                    row = sheet.CreateRow(0);
                  
                    for (int i = 0; i < columnCount; i++)
                    {
                        cell = row.CreateCell(i);
                        cell.SetCellValue(dataTable.Columns[i].ColumnName);
                    }


                    for (int i = 0; i < rowCount; i++)
                    {
                        row = sheet.CreateRow(i + 1);
                        for (int j = 0; j < columnCount; j++)
                        {
                            cell = row.CreateCell(j);
                            cell.SetCellValue(dataTable.Rows[i][j].ToString());
                        }
                    }

                    //保存路徑
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.Filter = "Excel 工作簿 (*.xls)|*.xls";
                    string strName = null;
                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        strName = saveFileDialog.FileName;
                    }
                    
                    using (fs = File.OpenWrite(@strName))
                    {
                        workbook.Write(fs);
                        result = true;
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                if (fs != null)
                {
                    fs.Close();
                }
                return false;
            }
        }

技術羣:1090519856

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章