引用NOPI 按excel模版 导出EXCEL类

按excel模版(字段带引出数据源字段编号)导出EXCEL

using System.Linq;
using System.Text;
using NPOI;
using NPOI.SS.UserModel;
using Kingdee.BOS.Util;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using Kingdee.BOS.Core.DependencyRules;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Core.Metadata.FieldElement;
using System.IO;
using System.Configuration;
using System.Data;
using Newtonsoft.Json;

namespace K3.App.Service.PlugIn
{
    public class ExportExcelEntity
    {
        public static string DynamicObjectCollectionTOExcel_CustomColumn(DynamicObjectCollection dt, string templateName, string fileName, string filePath)
        {
            string text = null;
            string result;
            string DataRow = "";
            string DataCell = "";
            try
            {
                string str = AppDomain.CurrentDomain.BaseDirectory + ("Bin\\DExcportFile\\");
                string text2 = fileName + ".xlsx";
                text = str + text2;
                string sourceFileName = AppDomain.CurrentDomain.BaseDirectory + ("Bin\\Exporttemplate\\" + templateName + ".xlsx");
                File.Copy(sourceFileName, text);
                FileAttributes fileAttributes = File.GetAttributes(text);
                fileAttributes &= ~FileAttributes.ReadOnly;
                File.SetAttributes(text, fileAttributes);
                FileStream fileStream = new FileStream(text, FileMode.Open, FileAccess.Read);
                string a = Path.GetExtension(text2).ToLower();
                IWorkbook workbook;
                if (a == ".xlsx")
                {
                    workbook = new XSSFWorkbook(fileStream);
                    XSSFCellStyle arg_B6_0 = (XSSFCellStyle)workbook.CreateCellStyle();
                    XSSFDataFormat arg_C3_0 = (XSSFDataFormat)workbook.CreateDataFormat();
                }
                else if (a == ".xls")
                {
                    workbook = new HSSFWorkbook(fileStream);
                    HSSFCellStyle arg_E9_0 = (HSSFCellStyle)workbook.CreateCellStyle();
                    HSSFDataFormat arg_F6_0 = (HSSFDataFormat)workbook.CreateDataFormat();
                }
                else
                {
                    workbook = null;
                }
                if (workbook == null)
                {
                    result = null;
                }
                else
                {
                    ISheet sheetAt = workbook.GetSheetAt(0);
                    IRow row = sheetAt.GetRow(0);
                    ISheet sheet = workbook.CreateSheet(fileName);
                    sheet.ShiftRows(0, 0, 1, true, false);
                    IRow row2 = sheet.CreateRow(0);
                    row2.Height = row.Height;//.set_Height(row.get_Height());
                    for (int i = (int)row.FirstCellNum; i < (int)row.LastCellNum; i++)
                    {

                        ICell cell = row.GetCell(i);
                        if (cell != null)
                        {
                            ICell cell2 = row2.CreateCell(i);
                            cell2.CellStyle = (cell.CellStyle);
                            cell2.SetCellType(cell.CellType);
                            cell2.SetCellValue(cell.StringCellValue); //row.GetCell(i).SetCellType(CellType.String);//.setCellType(cell.CELL_TYPE_STRING);
                        }
                    }
                    workbook.RemoveSheetAt(0);
                    IFont font = workbook.CreateFont();
                    // font.FontHeight = 9.0;
                    font.FontName = "Arial";
                    font.FontHeightInPoints = 10;
                    //font.FontName = "宋体";

                    //font.set_FontHeightInPoints(9.0);
                    //font.set_FontName("宋体");

                    IDataFormat dataformat = workbook.CreateDataFormat();
                    ICellStyle cellStyle = workbook.CreateCellStyle();
                    cellStyle.BorderBottom = BorderStyle.Thin;
                    cellStyle.BorderLeft = BorderStyle.Thin;
                    cellStyle.BorderRight = BorderStyle.Thin;
                    cellStyle.BorderTop = BorderStyle.Thin;

                    //cellStyle.WrapText = true;
                    cellStyle.VerticalAlignment = VerticalAlignment.Center;
                    /*dataformat.GetFormat("[DbNum2][$-804]General")汉字大写*/
                    //cellStyle.DataFormat = dataformat.GetFormat("#,##0.0");//dataformat.GetFormat("0.0;[Red]-0");
                    cellStyle.SetFont(font);
                    // cellStyle.Indention = 0;
                    // cellStyle.SetFont(font);
                    ICellStyle NubmerStyle = workbook.CreateCellStyle();
                    /*获取模板字段编号*/
                    IRow rowColumn = sheetAt.GetRow(1);
                    List<string> CellNames = new List<string>();
                    foreach (ICell cell in rowColumn)
                    {
                        CellNames.Add(cell.StringCellValue);//.ToString();
                    }

                    int Cellrow = 0;
                    foreach (DynamicObject dobj in dt)
                    {
                        IRow row3 = sheet.CreateRow(Cellrow + 1);
                        int n = 0;
                        DataRow = "";
                        /*按模板字段编号赋值*/
                        foreach (string CellName in CellNames)
                        {
                            ICell cell3 = row3.FirstOrDefault((ICell nol) => nol.ColumnIndex == n);
                            if (cell3 == null)
                            {
                                cell3 = row3.CreateCell(n);
                            }
                            //if(CellName==dobj.DynamicObjectType.Properties[""].Name)
                            string CellVal = dobj[CellName].ToString();
                            var PropertyType = dobj.DynamicObjectType.Properties[CellName].PropertyType.ToString();


                            if (dobj.DynamicObjectType.Properties[CellName].PropertyType == typeof(decimal))
                            {
                                if (string.IsNullOrEmpty(CellVal))
                                    CellVal = "0";
                                cell3.SetCellValue(double.Parse(CellVal));//.ToString("#,0.##"));//(double.Parse(CellVal).ToString("#,0.##"));
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(CellVal))
                                    CellVal = string.Empty;
                                cell3.SetCellValue(CellVal.Trim());
                            }
                            int columnWidth = sheet.GetColumnWidth(n);
                            int num = columnWidth / 256;
                            ICell cell4 = row3.GetCell(n);
                            int num2 = Encoding.Default.GetBytes(cell4.ToString()).Length;
                            if (num < num2 + 1)
                            {
                                num = num2 + 1;
                            }
                            if (num > 255)
                            {
                                num = 255;
                            }
                            sheet.SetColumnWidth(n, num * 256);
                            row3.GetCell(n).CellStyle = cellStyle;
                            n++;

                        }
                        Cellrow = Cellrow + 1;
                    }
                    using (FileStream fileStream2 = File.OpenWrite(filePath))
                    {
                        workbook.Write(fileStream2);
                        fileStream2.Close();
                    }
                    fileStream.Close();
                    File.Delete(text);//册除服务端导出文件
                    result = filePath;
                }
            }
            catch (Exception ex)
            {
                result = "返回信息:导出失败!错误信息:" + ex.Message + "</br>" + DataCell + "                                                     " + DataRow;
            }
            return result;
        }
        }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值