將所有公式加入excel

master
dk96 2025-01-06 13:47:19 +08:00
parent 26ea1e0883
commit eb7fc60023
1 changed files with 124 additions and 2 deletions

View File

@ -23,6 +23,7 @@ using NPOI.SS.Util;
using NPOI.XSSF.Streaming;
using System.IO;
using System.Linq;
using SixLabors.ImageSharp.Formats.Bmp;
namespace QuotationMaker.Controllers
{
@ -317,6 +318,9 @@ namespace QuotationMaker.Controllers
int acIndex = 9;
//此項目總金額位置
int sutTotalAcIndex = 10;
int noTaxTotalIndex = 11;
//優惠總價為置
int specTotalIndex = 12;
@ -325,6 +329,12 @@ namespace QuotationMaker.Controllers
//主項目編號
int itemNumber = 0;
//小計列位子陣列
int[] subGIndArr = new int[0];
//AC列位子陣列
int[] acGIndArr = new int[0];
//開始產生項目價格列
foreach (quotationMainItemDetail objMain in objDetail.quotationMainItemDetails)
{
@ -343,6 +353,7 @@ namespace QuotationMaker.Controllers
specTotalIndex++;
saRowIndex++;
saIndex++;
noTaxTotalIndex++;
foreach (quotationSubItem objSub in objMain.quotationSubItems) {
CopyRow((XSSFWorkbook)workbook, (XSSFSheet)sheet, editRowIndex, editRowIndex + 1);
@ -354,6 +365,7 @@ namespace QuotationMaker.Controllers
specTotalIndex ++;
saRowIndex++;
saIndex++;
noTaxTotalIndex++;
rowCount++;
}
@ -397,6 +409,9 @@ namespace QuotationMaker.Controllers
int rangeSubStart = editIndex + 1;
int rangeSubEnd = editIndex + 1;
int[] acRowIndArr = new int[0];
//合併子項目row
foreach (quotationSubItem objSub in objMain.quotationSubItems)
{
@ -458,9 +473,13 @@ namespace QuotationMaker.Controllers
row.Cells[5].SetCellValue(objSub.quotationSubItem_unitType);
//2025/1/6 更換為公式
//row.Cells[6].SetCellType(CellType.Numeric);
//row.Cells[6].SetCellValue(objSub.quotationSubItem_subTotal);
row.Cells[6].SetCellType(CellType.Formula);
string tmpFormSub = "D" + (editIndex + 1).ToString() + " * E" + (editIndex + 1).ToString();
row.Cells[6].SetCellFormula(tmpFormSub);
row.Cells[6].SetCellType(CellType.Numeric);
row.Cells[6].SetCellValue(objSub.quotationSubItem_subTotal);
if (objSub.quotationSubItem_subTotal.ToString().IndexOf('.') >= 0)
{
@ -494,10 +513,13 @@ namespace QuotationMaker.Controllers
specTotalIndex+=3;
saRowIndex += 3;
saIndex +=3;
noTaxTotalIndex += 3;
//複製小計欄
CopyRow((XSSFWorkbook)workbook, (XSSFSheet)sheet, subTotalIndex, editIndex);
subGIndArr = subGIndArr.ToList().Append(editIndex).ToArray();
row = (XSSFRow)sheet.GetRow(editIndex);
//row.Cells[6].SetCellValue("NTD " + (objMain.quotationMainItem_subTotal - acTotal).ToString("###,###"));
// 20250103 先刪除測試公式 row.Cells[6].SetCellType(CellType.Numeric);
@ -515,6 +537,8 @@ namespace QuotationMaker.Controllers
//複製AC欄
CopyRow((XSSFWorkbook)workbook, (XSSFSheet)sheet, acIndex, editIndex);
acGIndArr = acGIndArr.ToList().Append(editIndex).ToArray();
row = (XSSFRow)sheet.GetRow(editIndex);
//row.Cells[6].SetCellValue("NTD " + acTotal.ToString("###,###"));
row.Cells[6].SetCellType(CellType.Numeric);
@ -538,6 +562,8 @@ namespace QuotationMaker.Controllers
}
row.Cells[6].SetCellFormula(tmpForm2);
}
@ -568,6 +594,39 @@ namespace QuotationMaker.Controllers
//break;
}
//2025/1/6 新增總價改為公式
row = (XSSFRow)sheet.GetRow(noTaxTotalIndex);
row.Cells[6].SetCellType(CellType.Formula);
string tmpFormNoTaxTotal = "";
foreach (int s in subGIndArr)
{
if (tmpFormNoTaxTotal != "")
{
tmpFormNoTaxTotal += " + ";
}
tmpFormNoTaxTotal += "G" + (s + 1);
}
foreach (int s in acGIndArr)
{
if (tmpFormNoTaxTotal != "")
{
tmpFormNoTaxTotal += " + ";
}
tmpFormNoTaxTotal += "G" + (s + 1);
}
row.Cells[6].SetCellFormula(tmpFormNoTaxTotal);
//刪除不需要的優惠價格欄
if (objDetail.quotation_specTotal == 0) {
@ -613,6 +672,8 @@ namespace QuotationMaker.Controllers
saRowIndex--;
}
string agent = Request.Headers["User-Agent"].ToString();
string post_pc = "win";
@ -719,6 +780,67 @@ namespace QuotationMaker.Controllers
XSSFFormulaEvaluator.EvaluateAllFormulaCells(workbook);
int grandTotalIndex = saRowIndex - 2;
//2025/1/6 將VAT改為公式
row = (XSSFRow)sheet.GetRow(grandTotalIndex - 1);
row.Cells[6].SetCellType(CellType.Formula);
string tmpFormVat = "Round(G" + (grandTotalIndex - 1).ToString() + " * 0.05, 0)";
row.Cells[6].SetCellFormula(tmpFormVat);
//if (objDetail.quotation_specTotal <= 0)
//{
// row = (XSSFRow)sheet.GetRow(noTaxTotalIndex);
// row.Cells[6].SetCellType(CellType.Formula);
// string tmpFormVat = "Round(G" + (noTaxTotalIndex - 1).ToString() + " * 0.05, 0)";
// row.Cells[6].SetCellFormula(tmpFormVat);
//}
//else
//{
// row = (XSSFRow)sheet.GetRow(noTaxTotalIndex - 2);
// row.Cells[6].SetCellType(CellType.Formula);
// string tmpFormVat = "Round(G" + (noTaxTotalIndex).ToString() + " * 0.05, 0)";
// row.Cells[6].SetCellFormula(tmpFormVat);
//}
//2025/1/6 將含稅總價改為公式
//if (objDetail.quotation_specTotal <= 0)
//{
// noTaxTotalIndex = noTaxTotalIndex - 2;
//}
//else {
// noTaxTotalIndex = noTaxTotalIndex - 1;
//}
row = (XSSFRow)sheet.GetRow(grandTotalIndex);
row.Cells[6].SetCellType(CellType.Formula);
string tmpFormTaxTotal = "";
tmpFormTaxTotal = "Round(G" + (grandTotalIndex - 1).ToString() + " + " + "G" + (grandTotalIndex).ToString() + ", 0)";
//if (objDetail.quotation_specTotal > 0)
//{
// tmpFormTaxTotal = "G" + (noTaxTotalIndex - 2).ToString() + " + " + "G" + (noTaxTotalIndex).ToString();
//}
//else
//{
// tmpFormTaxTotal = "Round(G" + (noTaxTotalIndex - 1).ToString() + " + " + "G" + (noTaxTotalIndex).ToString() + ", 0)";
//}
row.Cells[6].SetCellFormula(tmpFormTaxTotal);
XSSFFormulaEvaluator.EvaluateAllFormulaCells(workbook);
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
ms.Flush();