c#使用NPOI批量導出數據到excel表格裏

背景

我之前是做的Java開發的相關工作,c#開發還是第一次。

公司有這個需求,只好研究一下了。

我接手的是別人開發過兩個報表的小玩意,萬事開頭難,剛開始爲了搞清楚這些代碼的意思走了不少彎路,下面說說我的理解,有不對的地方希望指出來,謝謝。

開始

我們先把準備工作做好,去官網下載NPOI,其他的我隨後解釋

NPOI官網下載地址:https://www.nuget.org/packages/NPOI/

在這裏可以看到最新的版本是NPOI2.4.1版本

下載壓縮包——在這找到Download package ,下載壓縮包

添加引用

下載完之後,解壓,打開解壓文件夾下的release文件夾,你會看到有net20和net40文件夾,這裏我們打開net40,可以看到一些dll文件,我們需要的就是引用NPOI.dll。

在這裏我用的是Visual Studio community 2019版本,這個版本是學生、開發源代碼參與者和個人免費使用的。

我們打開VS界面,添加引用,選擇剛剛那個dll,並確定。添加完成後,可以在引用裏面看到。至於其他的引用你們可以照着這個來,數據庫用的是mysql數據庫,其他的引用小編也不是都知道,哈。

useing一些必要的東東       然後useing 這些玩意

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

然後就可以開始開發了,項目結構自己定,導出數據的界面可自由設計合適的

設計可視化界面

進到設計界面視圖,選視圖-——選擇工具箱會出現這個頁面,左側是組件,鼠標拖動左側組件到tabPage11就可自由設計你想要的頁面了,在這我列舉了幾個例子,都是表頭上用來篩選數據的,

界面的一些簡單設置

除此之外,你需要把表格和表頭加上去,標了步驟,首先找屬性裏的雜項,這一列最右邊(集合)框裏有三個點 ...

點擊會出現一個編輯列的框,在這可設置表頭文字,表頭寬度,表頭文字高度,數據來源就是數據庫裏的字段,儘量和查詢的字段保持一致

可自由添加列,根據需要添加,這個就是NPOI查詢到的數據的展示界面

表格上面設置有  查詢  和   導出   按鈕,用來查詢數據和導出數據到ecxel表格

ok,設計完界面,下面可以進行實際開發工作了

查詢代碼

這段代碼就是對應的查詢代碼,模糊查詢就是表頭上用來篩選數據的東東

就是這些,不多說上圖

   ///應收/應付
        private void button1_Click(object sender, EventArgs e)
        {
//charset=gb2312;是編碼格式,數據庫用戶名一般都是root
            string connStr = "server=服務器IP;user id=root;password=數據庫密碼;database=數據庫名;charset=gb2312;";
            string query = "select g.tour_groupno,p.tour_companyName,g.tour_lineName,h.tour_name as tour_jdId,g.tour_countMan,g.tour_countChild," +
            "(CASE j.tour_type WHEN 0 THEN '雲南線' WHEN 1 THEN '泰國線' WHEN 2 THEN '貴州' WHEN 3 THEN '四川' WHEN 4 THEN '海南' WHEN 5 THEN '西北'WHEN 6 THEN '桂林' ELSE 'null' END) as tour_type," +
                             "p.tour_cwName,t.tour_date,q.tour_money,p.tour_money as tour_moneytot,h.tour_ysmoney,h.tour_alreadymoney,p.tour_mark,p.tour_cwStatus from " +
                             "(select tour_id,tour_date from tour_payingsearch " +
                             "where tour_date between '" + date1.Value.ToString("yyyy-MM-dd") + "' and '" + date2.Value.ToString("yyyy-MM-dd") + "' ";
            query += "and tour_cid = 4 and tour_cate=" + cb1.SelectedValue + " ) as t " +
                            "inner join tour_paying p on p.tour_id=t.tour_id ";
             //模糊查詢公司名稱
             if (!string.IsNullOrEmpty(textBox2.Text))
                query += "and p.tour_companyName like '%" + textBox2.Text.Trim() + "%' ";
            query += "inner join tour_payinginfo q on p.tour_Id = q.tour_payingid " +
                            "inner join tour_group g on g.tour_id = q.tour_groupId ";
            //模糊查詢團號
            if (!string.IsNullOrEmpty(textBox1.Text))
                query += "and g.tour_groupno like '%" + textBox1.Text.Trim() + "%'";
            //模糊查詢線路名稱
            if (!string.IsNullOrEmpty(textBox6.Text))
                query += "and g.tour_lineName like '%" + textBox6.Text.Trim() + "%'";
            //模糊查詢備註名稱
            if (!string.IsNullOrEmpty(textBox22.Text))
                query += "and p.tour_mark like '%" + textBox22.Text.Trim() + "%' ";
            query += "inner join tour_groupcompanybill h on g.tour_id = h.tour_gid and q.tour_billId=h.tour_id ";
            //模糊查詢工作人員名稱
            if (!string.IsNullOrEmpty(textBox30.Text))
                query += "and h.tour_name like '%" + textBox30.Text.Trim() + "%' ";
            query += "inner join tour_groupsearch j on j.tour_Id = g.tour_id ";
            //類型模糊查詢
            if (!string.IsNullOrEmpty(textBox24.Text))
                query += "and (CASE j.tour_type WHEN 0 THEN '雲南' WHEN 1 THEN '泰國' WHEN 2 THEN '貴州' WHEN 3 THEN '四川' WHEN 4 THEN '海南' WHEN 5 THEN '西北'WHEN 6 THEN '桂林' ELSE 'null' END) like '%" + textBox24.Text.Trim() + "%' ";

            try
            {
                MySqlConnection conn = new MySqlConnection(connStr);
                conn.Open();
                MySqlCommand myCommand = new MySqlCommand(query, conn);
                myCommand.ExecuteNonQuery();
                MySqlDataReader myDataReader = myCommand.ExecuteReader();
                dt = ConvertDataReaderToDataTable(myDataReader);
                sumdt();
                gd1.DataSource = dt;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

表格設置代碼

此外,有一個重要的對datatable的設置,我理解爲對錶格的一些設置,看圖

巴拉巴拉這一段,我也不是很清楚什麼意思,總之,是對錶格的設置就對了

 private static DataTable ConvertDataReaderToDataTable(MySqlDataReader reader)
        {
            try
            {
                DataTable objDataTable = new DataTable();
                int intFieldCount = reader.FieldCount;
                for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
                {
                    objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter));
                }
                objDataTable.BeginLoadData();

                object[] objValues = new object[intFieldCount];
                while (reader.Read())
                {
                    reader.GetValues(objValues);
                    objDataTable.LoadDataRow(objValues, true);
                }
                reader.Close();
                objDataTable.EndLoadData();
                return objDataTable;
            }
            catch (Exception ex)
            {
                throw new Exception("轉換出錯!"+ex.Message);
            }

        }

統計表格數據

此外有一個統計功能,就是對一列數據進行統計數量,這個裏面有一些必要的設置

 private void sumdt()
        {
            if (dt != null)
            {
                int cnt = 0;
                decimal tot = 0.00M;//單團覈銷金額
                decimal ystot = 0.00M;//應收金額
                decimal atot = 0.00M;//已收金額
                decimal huizong = 0.00M;//彙總金額
                decimal countMan = 0M;//大人人數
                decimal countChild = 0M;//小孩人數
                foreach (DataRow dr in dt.Rows)
                {
                    cnt++;
                    tot += Convert.ToDecimal(dr["tour_money"]);
                    ystot += Convert.ToDecimal(dr["tour_ysmoney"]);
                    atot += Convert.ToDecimal(dr["tour_alreadymoney"]);
                    huizong += Convert.ToDecimal(dr["tour_moneytot"]);
                    countMan += Convert.ToDecimal(dr["tour_countMan"]);
                    countChild += Convert.ToDecimal(dr["tour_countChild"]);
                }
                DataRow dr1 = dt.NewRow();
                dr1["tour_groupno"] = "總計" + cnt + "條";
                dr1["tour_money"] = tot;
                dr1["tour_ysmoney"] = ystot;
                dr1["tour_alreadymoney"] = atot;
                dr1["tour_moneytot"] = huizong;
                dr1["tour_countMan"] = countMan;
                dr1["tour_countChild"] = countChild;
                dt.Rows.Add(dr1);
            }
        }

對錶格的定義

這個dt!=null的dt是對錶格的簡稱,不定義下面沒法用,是在這設置的

導出數據

在這裏我就貼幾個字段的查詢好了,要不然代碼太多了

    private void button2_Click(object sender, EventArgs e)
        {
            if (dt == null) return;
            try
            {
                //設置導出格式,這裏設置是.xls
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.CreatePrompt = true;
                saveFileDialog.Title = "Export Excel File";
                saveFileDialog.ShowDialog();
                if (saveFileDialog.FileName == "")
                    return;
                FileStream file = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.ReadWrite);
                //創建workbook
                var hssfworkbook = new HSSFWorkbook();
                IFont font = hssfworkbook.CreateFont();
                font.FontHeightInPoints = 10;
                IFont tfont = hssfworkbook.CreateFont();
                tfont.FontHeightInPoints = 14;
                tfont.Boldweight = (short)FontBoldWeight.BOLD;
                IFont bfont = hssfworkbook.CreateFont();
                bfont.FontHeightInPoints = 10;
                bfont.Boldweight = (short)FontBoldWeight.BOLD;
                //創建sheet
                HSSFSheet sheet1 = (HSSFSheet)hssfworkbook.CreateSheet("明細");
                ICellStyle blackBorder = hssfworkbook.CreateCellStyle();
                blackBorder.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
                blackBorder.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
                blackBorder.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
                blackBorder.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
                blackBorder.SetFont(font);
                //表頭
                ICellStyle moneystyle = hssfworkbook.CreateCellStyle();
                moneystyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
                moneystyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
                moneystyle.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
                moneystyle.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
                moneystyle.SetFont(font);
                IDataFormat format = hssfworkbook.CreateDataFormat();
                moneystyle.DataFormat = format.GetFormat("#,##0.00");

                ICellStyle intstyle = hssfworkbook.CreateCellStyle();
                intstyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
                intstyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
                intstyle.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
                intstyle.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
                intstyle.SetFont(font);
                IDataFormat format2 = hssfworkbook.CreateDataFormat();
                intstyle.DataFormat = format2.GetFormat("0");

                ICellStyle styleMiddle = hssfworkbook.CreateCellStyle();
                styleMiddle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
                styleMiddle.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
                styleMiddle.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
                styleMiddle.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
                styleMiddle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;
                styleMiddle.VerticalAlignment = VerticalAlignment.CENTER;
                styleMiddle.SetFont(font);

                ICellStyle titlestyle = hssfworkbook.CreateCellStyle();
                titlestyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
                titlestyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
                titlestyle.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
                titlestyle.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
                titlestyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
                titlestyle.VerticalAlignment = VerticalAlignment.CENTER;
                titlestyle.SetFont(tfont);
                //對錶頭的設置,導出的表頭就是這裏設置的
                int rowno = 0;
                var header = sheet1.CreateRow(rowno);
                var hcell = header.CreateCell(0);
                sheet1.SetColumnWidth(0, 24 * 256);
                hcell.SetCellValue("團號");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(1);
                sheet1.SetColumnWidth(1, 50 * 256);
                hcell.SetCellValue("公司名稱");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(2);
                sheet1.SetColumnWidth(2, 12 * 256);
                hcell.SetCellValue("線路名稱");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(3);
                sheet1.SetColumnWidth(3, 11 * 256);
                hcell.SetCellValue("類型");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(4);
                sheet1.SetColumnWidth(4, 11 * 256);
                hcell.SetCellValue("計調");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(5);
                sheet1.SetColumnWidth(5, 11 * 256);
                hcell.SetCellValue("財務");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(6);
                sheet1.SetColumnWidth(6, 14 * 256);
                hcell.SetCellValue("日期");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(7);
                sheet1.SetColumnWidth(7, 16 * 256);
                hcell.SetCellValue("大人人數");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(8);
                sheet1.SetColumnWidth(8, 16 * 256);
                hcell.SetCellValue("小孩人數");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(9);
                sheet1.SetColumnWidth(9, 16 * 256);
                hcell.SetCellValue("單團覈銷金額");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(10);
                sheet1.SetColumnWidth(10, 16 * 256);
                hcell.SetCellValue("彙總金額");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(11);
                sheet1.SetColumnWidth(11, 16 * 256);
                hcell.SetCellValue("應收金額");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(12);
                sheet1.SetColumnWidth(12, 16 * 256);
                hcell.SetCellValue("已收金額");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(13);
                sheet1.SetColumnWidth(13, 40 * 256);
                hcell.SetCellValue("備註");
                hcell.CellStyle = titlestyle;
                hcell = header.CreateCell(14);
                sheet1.SetColumnWidth(14, 8 * 256);
                hcell.SetCellValue("審覈");  
                hcell.CellStyle = titlestyle;

                //表內數據就是這裏
                foreach (DataRow dr in dt.Rows)
                {
                    rowno++;
                    var bottom = sheet1.CreateRow(rowno);
                    var botcell = bottom.CreateCell(0);
                    botcell.SetCellValue(dr["tour_groupno"].ToString());
                    botcell.CellStyle = blackBorder;
                    botcell = bottom.CreateCell(1);
                    botcell.SetCellValue(dr["tour_companyName"].ToString());
                    botcell.CellStyle = blackBorder;
                    botcell = bottom.CreateCell(2);
                    botcell.SetCellValue(dr["tour_lineName"].ToString());
                    botcell.CellStyle = blackBorder;
                    botcell = bottom.CreateCell(3);
                    botcell.SetCellValue(dr["tour_type"].ToString());
                    botcell.CellStyle = blackBorder;
                    botcell = bottom.CreateCell(4);//////////////////////////
                    botcell.SetCellValue(dr["tour_jdId"].ToString());
                    botcell.CellStyle = blackBorder;
                    botcell = bottom.CreateCell(5);
                    botcell.SetCellValue(dr["tour_cwName"].ToString());
                    botcell.CellStyle = blackBorder;
                    botcell = bottom.CreateCell(6);
                    botcell.SetCellValue(dr["tour_date"]!=DBNull.Value?Convert.ToDateTime(dr["tour_date"]).ToString("yyyy-MM-dd"):"");
                    botcell.CellStyle = blackBorder;
                    botcell = bottom.CreateCell(7);
                    botcell.SetCellValue(dr["tour_countMan"].ToString());
                    botcell.CellStyle = blackBorder;
                    botcell = bottom.CreateCell(8);
                    botcell.SetCellValue(dr["tour_countChild"].ToString());
                    botcell.CellStyle = blackBorder;
                    botcell = bottom.CreateCell(9);
                    botcell.SetCellValue(Convert.ToDouble(dr["tour_money"]));
                    botcell.CellStyle = moneystyle;
                    botcell = bottom.CreateCell(10);
                    botcell.SetCellValue(dr["tour_moneytot"] != DBNull.Value ? Convert.ToDouble(dr["tour_moneytot"]) : Convert.ToDouble(dr["tour_money"]));
                    botcell.CellStyle = moneystyle;
                    botcell = bottom.CreateCell(11);
                    botcell.SetCellValue(Convert.ToDouble(dr["tour_ysmoney"]));
                    botcell.CellStyle = moneystyle;
                    botcell = bottom.CreateCell(12);
                    botcell.SetCellValue(Convert.ToDouble(dr["tour_alreadymoney"]));
                    botcell.CellStyle = moneystyle;
                    botcell = bottom.CreateCell(13);
                    botcell.SetCellValue(dr["tour_mark"].ToString());
                    botcell.CellStyle = blackBorder;
                    botcell = bottom.CreateCell(14);
                    botcell.SetCellValue(dr["tour_cwStatus"].ToString());
                    botcell.CellStyle = blackBorder;
                 
                }
                //轉化爲字節數組
                hssfworkbook.Write(file);
                file.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("導出文件出錯!" + ex.Message);
            }
        }

這邊完成就可以去測試了,我這是完成了這個項目沒問題的,可以正常查詢數據,導出數據

如果在開發NPOI批量導出數據過程中有問題,可評論,我看到會回覆的。

若實在不行,可評論問我要源碼,今天就寫到這裏,希望能幫到各位。

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