NPOI导出Excel百分比格式

NPOI导出Excel百分比格式

using

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

代码实现

        public int DataTableToExcel(string fileName, DataTable[] datas, bool isColumnWritten)
        {
            ISheet sheet = null;

            FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            IWorkbook workbook = null;

            if (fileName.IndexOf(".xlsx") > 0) // 2007版本  
                workbook = new XSSFWorkbook();
            else if (fileName.IndexOf(".xls") > 0) // 2003版本  
                workbook = new HSSFWorkbook();

            if (workbook == null)
            {
                return -1;
            }

            //百分比格式
            ICellStyle cellStylePercent = workbook.CreateCellStyle();
            cellStylePercent.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");

            int step = 0;

            for (int n = 0; n < datas.Length; n++)
            {
                int count = 0;
                DataTable data = datas[n];
                sheet = workbook.CreateSheet(data.TableName);


                if (isColumnWritten == true) //写入DataTable的列名  
                {
                    IRow row = sheet.CreateRow(0);
                    for (int j = 0; j < data.Columns.Count; ++j)
                    {
                        row.CreateCell(j).SetCellValue(data.Columns[j].ColumnName);
                    }
                    count++;
                }

                for (int i = 0; i < data.Rows.Count; i++)
                {
                    IRow row = sheet.CreateRow(count);
                    for (int j = 0; j < data.Columns.Count; ++j)
                    {
                        if (data.Rows[i][j] is double || data.Rows[i][j] is int)
                        {
                            if (data.Rows[i][j] is int)
                            {
                                row.CreateCell(j).SetCellValue(Convert.ToDouble((int)data.Rows[i][j]));
                            }
                            else
                            {
                                row.CreateCell(j).SetCellValue((double)data.Rows[i][j]);
                            }
                            if (data.Columns[j].ColumnName=="列名")//需要百分比格式的列名
                            {
                                row.Cells[j].CellStyle = cellStylePercent;
                            }
                        }
                        else
                        {
                            row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString());
                        }
                    }
                    ++count;
                }
                //调整列宽
                for (int j = 0; j < data.Columns.Count; j++)
                {
                    sheet.AutoSizeColumn(j);

                    if (sheet.GetColumnWidth(j) < 2048)
                    {
                        sheet.SetColumnWidth(j, 2048);
                    }
                }
                Console.WriteLine("写入" + data.TableName + "成功");
            }
            using (fs)
            {
                workbook.Write(fs); //写入到excel  
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摩特男爵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值