c# datatable转成excel类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string sql = "实际sql语句";
            DataTable dt = OracleHelper.ExecuteDataTable(sql);
            ExcelHepler excelHepler = new ExcelHepler();
 
            ArrayList arr_Get_Att = new ArrayList();
 
            //附件
            if (dt != null && dt.Rows.Count > 0)
            {
                string name = "汇总表-" + DateTime.Now.ToString("yyyyMMdd");
excel
                FileStream file = new FileStream(@"D:\bpmUnfinished\" + name + ".xls", FileMode.Create, System.IO.FileAccess.Write);
                
                byte[] bytes = excelHepler.DataTable2Excel(dt, name);
                file.Write(bytes, 0, bytes.Length);
                file.Close();
                arr_Get_Att.Add(@"D:\bpmUnfinished\" + name + ".xls");
               
            }

  

1
ExcelHepler类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
 
namespace MfgLotState
{
    class ExcelHepler
    {
        /// <summary>
        /// 类版本
        /// </summary>
        public string version
        {
            get { return "0.1"; }
        }
 
        readonly int EXCEL03_MaxRow = 65535;
 
        /// <summary>
        /// 将DataTable转换为excel2003格式。
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public byte[] DataTable2Excel(DataTable dt, string sheetName)
        {
 
            IWorkbook book = new HSSFWorkbook();
            if (dt.Rows.Count < EXCEL03_MaxRow)
                DataWrite2Sheet(dt, 0, dt.Rows.Count - 1, book, sheetName);
            else
            {
                int page = dt.Rows.Count / EXCEL03_MaxRow;
                for (int i = 0; i < page; i++)
                {
                    int start = i * EXCEL03_MaxRow;
                    int end = (i * EXCEL03_MaxRow) + EXCEL03_MaxRow - 1;
                    DataWrite2Sheet(dt, start, end, book, sheetName + i.ToString());
                }
                int lastPageItemCount = dt.Rows.Count % EXCEL03_MaxRow;
                DataWrite2Sheet(dt, dt.Rows.Count - lastPageItemCount, lastPageItemCount, book, sheetName + page.ToString());
            }
            MemoryStream ms = new MemoryStream();
            book.Write(ms);
            return ms.ToArray();
        }
        private void DataWrite2Sheet(DataTable dt, int startRow, int endRow, IWorkbook book, string sheetName)
        {
            ISheet sheet = book.CreateSheet(sheetName);
            IRow header = sheet.CreateRow(0);
            int lengthTmp = 10;
            int[] lengthColumn = new int[dt.Columns.Count];
 
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                ICell cell = header.CreateCell(i);
                string val = dt.Columns[i].Caption ?? dt.Columns[i].ColumnName;
                cell.SetCellValue(val);
 
                lengthTmp = Encoding.Default.GetBytes(val).Length;
                if (lengthColumn[i] < lengthTmp)
                {
                    lengthColumn[i] = lengthTmp;
                }
            }
 
            int rowIndex = 1;
            for (int i = startRow; i <= endRow; i++)
            {
                DataRow dtRow = dt.Rows[i];
                IRow excelRow = sheet.CreateRow(rowIndex++);
                for (int j = 0; j < dtRow.ItemArray.Length; j++)
                {
                    excelRow.CreateCell(j).SetCellValue(dtRow[j].ToString());
 
                    lengthTmp = Encoding.Default.GetBytes(dtRow[j].ToString()).Length;
                    if (lengthColumn[j] < lengthTmp)
                    {
                        lengthColumn[j] = lengthTmp;
                    }
                }
            }
 
            for(int columnNum = 0; columnNum<dt.Columns.Count; columnNum++)
            {
                int columnWidth = sheet.GetColumnWidth(columnNum) / 256;
 
                columnWidth = lengthColumn[columnNum];
 
                if(columnWidth>100)
                {
                    columnWidth = 100;
                }
                else
                {
                    columnWidth = columnWidth + columnWidth/10;
                }
                
                sheet.SetColumnWidth(columnNum, columnWidth * 278);
            }
 
        }
    }
}

  

posted @   小严不言慢  阅读(96)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示