using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using System.IO;
using NPOI.SS.Util;
using NPOI.HSSF.Util;
using System.Collections;
using System.Data;
using NPOI.XSSF.UserModel;
using DataLinkMesWork;
using NPOI.XSSF.Streaming;
using Spire.Xls;

namespace projectUse
{
    /// <summary>
    /// 文件代码
    /// 
    /// 01 计划工时模板
    /// 02 外协加工任务单
    /// 03 外购备料请款
    /// </summary>
    public partial class WriteExcelNPOI_2005
    {


        /// <summary>
        /// 根据模板填写报表
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="bytes"></param>
        /// <param name="fileExtension"></param>
        /// <returns></returns>
        public static bool WriteExcel_2005(DataSet ds, out byte[] bytes, out string fileExtension)
        {
            fileExtension = "xls";
            ISheet sheet = null;
            bytes = null;
            // 模板数据
            DataTable dt1;
            // 表头数据
            DataTable dt2;
            // 表格1数据
            DataTable dt3;
            // 表格2数据
            DataTable dt4;
            // 表格3数据
            DataTable dt5;
            try
            {

                dt1 = ds.Tables[0]; // 模板数据
                dt2 = ds.Tables[1]; // 表头数据
                dt3 = ds.Tables[2]; // 表格1数据
                dt4 = ds.Tables[3]; // 表格2数据
                dt5 = ds.Tables[4]; // 表格3数据

               
                string fileNameDownLoad = "";
                string tablecode = "2005";
                byte[] bytesFile;
                tablecode = dt1.Rows[0]["tabletype"].ToString();
                int rowCount = dt1.Rows.Count + dt2.Rows.Count + dt3.Rows.Count + dt4.Rows.Count + dt5.Rows.Count;//行数
                if (rowCount > 20000)
                {
                    XSSFWorkbook wk = new XSSFWorkbook();
                    fileExtension = "xlsx";
                    sheet = wk.CreateSheet("Sheet1");

                    ICell cell;
                    IRow row = sheet.CreateRow(0);//excel第一行设为列头
                  

                }
                else
                {
                    HSSFWorkbook wk = new HSSFWorkbook();
                    sheet = wk.CreateSheet("Sheet1");

                    ICell cell;
                    IRow row = sheet.CreateRow(0);//excel第一行设为列头
                    fileExtension = "xls";
               
                    WriteHead2005FirstRow(sheet, dt2);
                    OutExcelFileMemoryStream(wk, out bytes);
                }



              
                return true;
            }
            catch
            {
                return false;
            }
        }

        
        private static void WriteHead2005FirstRow(ISheet sheet, DataTable dt)
        {
            try
            {
                string tableFirstRowName = dt.Rows[0]["tableTitle"].ToString();
                IRow row1 = sheet.GetRow(0);
                ICell cell1 = row1.CreateCell(0);
                //合并单元格 CellRangeAddress四个参数为：起始行，结束行，起始列，结束列
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10));
                cell1.SetCellValue(tableFirstRowName);

                string orderCode = dt.Rows[0]["订单号"].ToString();
                string enignerType = dt.Rows[0]["发动机型号"].ToString();
                string enignerCode = dt.Rows[0]["发动机号"].ToString();
                string uplineTime = dt.Rows[0]["上线时间"].ToString();

                // 订单号
                IRow row2 = sheet.GetRow(1);
                ICell cell2 = row2.CreateCell(1);
                sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 1));
                cell2.SetCellValue("订单号");
                IRow row3 = sheet.GetRow(1);
                ICell cell3 = row3.CreateCell(1);
                sheet.AddMergedRegion(new CellRangeAddress(1, 1, 2, 4));
                cell3.SetCellValue(orderCode);
                // 发动机号
                IRow row4 = sheet.GetRow(1);
                ICell cell4 = row4.CreateCell(1);
                sheet.AddMergedRegion(new CellRangeAddress(1, 1, 5, 6));
                cell4.SetCellValue("发动机号");
                IRow row5 = sheet.GetRow(1);
                ICell cell5 = row5.CreateCell(1);
                sheet.AddMergedRegion(new CellRangeAddress(1, 1, 7, 10));
                cell5.SetCellValue(enignerCode);



                // 订单号
                IRow row22 = sheet.GetRow(2);
                ICell cell22 = row22.CreateCell(2);
                sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 1));
                cell22.SetCellValue("发动机型号");
                IRow row32 = sheet.GetRow(2);
                ICell cell32 = row32.CreateCell(2);
                sheet.AddMergedRegion(new CellRangeAddress(2, 2, 2, 4));
                cell32.SetCellValue(enignerType);
                // 发动机号
                IRow row42 = sheet.GetRow(2);
                ICell cell42 = row42.CreateCell(2);
                sheet.AddMergedRegion(new CellRangeAddress(2, 2, 5, 6));
                cell42.SetCellValue("上线时间");
                IRow row52 = sheet.GetRow(2);
                ICell cell52 = row52.CreateCell(2);
                sheet.AddMergedRegion(new CellRangeAddress(2, 2, 7, 10));
                cell52.SetCellValue(uplineTime);


            }
            catch
            {

            }
        }
        private static void WriteHead2005SecondRow(ISheet sheet, DataTable dt)
        {
            try
            {
                string tableFirstRowName = dt.Rows[0]["tableTitle"].ToString();
                IRow row = sheet.GetRow(0);
                ICell cell = row.CreateCell(0);
                //合并单元格 CellRangeAddress四个参数为：起始行，结束行，起始列，结束列
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10));
                cell.SetCellValue(tableFirstRowName);
            }
            catch
            {

            }
        }
        static private void OutExcelFileMemoryStream(IWorkbook wk, out byte[] bytes)
        {
            MemoryStream ms = new MemoryStream();
            wk.Write(ms);
            bytes = ms.ToArray();
        }
    }
}
