fz

 public class Do
    {
        XSSFWorkbook hssfworkbook;
        XSSFWorkbook templateLeft;
        XSSFWorkbook templateRight;
        List<string[]> data = new List<string[]>();
        string filePath;
        public Do(string filePath)
        {
            this.filePath = filePath;
            this.ReadTemplate();
        }

        public bool Exchange()
        {
            bool flag = true;
            try
            {
                this.ReadExcel();
                var sheetLeft = templateLeft.GetSheetAt(0);
                var sheetRight = templateRight.GetSheetAt(0);
                ICell cell;
                foreach (var arr in data)
                {
                    if (arr == null)
                        continue;
                    #region left
                    cell = this.SearchSell(sheetLeft, 1, 2);
                    cell.SetCellValue(arr[1]);
                    cell = this.SearchSell(sheetLeft, 2, 6);
                    cell.SetCellValue(arr[24]);
                    cell = this.SearchSell(sheetLeft, 5, 1);
                    cell.SetCellValue(arr[3]);
                    cell = this.SearchSell(sheetLeft, 5, 2);
                    cell.SetCellValue(arr[4]);
                    cell = this.SearchSell(sheetLeft, 5, 3);
                    cell.SetCellValue(arr[5]);
                    cell = this.SearchSell(sheetLeft, 5, 4);
                    cell.SetCellValue(arr[6]);
                    cell = this.SearchSell(sheetLeft, 5, 5);
                    cell.SetCellValue(arr[2]);
                    cell = this.SearchSell(sheetLeft, 5, 6);
                    cell.SetCellValue(arr[12]);
                    #endregion

                    #region right
                    cell = this.SearchSell(sheetRight, 1, 2);
                    cell.SetCellValue(arr[1]);
                    cell = this.SearchSell(sheetRight, 2, 6);
                    cell.SetCellValue(arr[24]);
                    cell = this.SearchSell(sheetRight, 5, 1);
                    cell.SetCellValue(arr[8]);
                    cell = this.SearchSell(sheetRight, 5, 2);
                    cell.SetCellValue(arr[9]);
                    cell = this.SearchSell(sheetRight, 5, 3);
                    cell.SetCellValue(arr[10]);
                    cell = this.SearchSell(sheetRight, 5, 4);
                    cell.SetCellValue(arr[11]);
                    cell = this.SearchSell(sheetRight, 5, 5);
                    cell.SetCellValue(arr[7]);
                    cell = this.SearchSell(sheetRight, 5, 6);
                    cell.SetCellValue(arr[18]);
                    #endregion
                    this.WirteToFiles(arr[1]);
                }
            }
            catch (Exception ex)
            {
                flag = false;
            }
            return flag;
        }

        private void ReadExcel()
        {
            using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                hssfworkbook = new XSSFWorkbook(file);
                if (hssfworkbook != null)
                {
                    ISheet sheet = hssfworkbook.GetSheetAt(0);

                    for (int i = 3; i < sheet.LastRowNum; i++)
                    {
                        IRow row = sheet.GetRow(i);
                        string[] dataRows = new string[25];
                        for (int j = 0; j < row.LastCellNum; j++)
                        {
                            ICell cell = row.GetCell(j);
                            dataRows[j] = string.IsNullOrWhiteSpace(cell.ToString()) ? string.Empty : cell.NumericCellValue.ToString();
                        }
                        data.Add(dataRows);
                    }
                }
            }
        }

        private void ReadTemplate()
        {
            string filePathLeft = @".\template\left.xlsx";
            string filePathRight = @".\template\left.xlsx";
            using (FileStream file = new FileStream(filePathLeft, FileMode.Open, FileAccess.Read))
            {
                templateLeft = new XSSFWorkbook(file);
            }
            using (FileStream file = new FileStream(filePathRight, FileMode.Open, FileAccess.Read))
            {
                templateRight = new XSSFWorkbook(file);
            }
        }

        private ICell SearchSell(ISheet sheet, int rowNumber, int columnNumber)
        {
            var row = sheet.GetRow(rowNumber);
            return row.GetCell(columnNumber);
        }

        private void WirteToFiles(string fileName)
        {
            using (FileStream fileLeft = new FileStream(string.Format(@"export\{0}左.xlsx", fileName), FileMode.Create))
            {
                templateLeft.Write(fileLeft);
            }
            using (FileStream fileRight = new FileStream(string.Format(@"export\{0}右.xlsx", fileName), FileMode.Create))
            {
                templateRight.Write(fileRight);
            }
        }
    }

 

转载于:https://www.cnblogs.com/gongziwuji/p/5175540.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值