master
嘉祥 詹 2024-09-15 00:53:16 +08:00
parent 3b68b997e5
commit e575cab719
4 changed files with 154 additions and 0 deletions

View File

@ -19,6 +19,8 @@ using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel; using NPOI.HSSF.UserModel;
using System.Text; using System.Text;
using System.Web; using System.Web;
using NPOI.SS.Util;
using NPOI.XSSF.Streaming;
namespace QuotationMaker.Controllers namespace QuotationMaker.Controllers
{ {
@ -154,6 +156,51 @@ namespace QuotationMaker.Controllers
row = (XSSFRow)sheet.GetRow(20); row = (XSSFRow)sheet.GetRow(20);
row.Cells[0].SetCellValue(quotation_grandTotalStr); row.Cells[0].SetCellValue(quotation_grandTotalStr);
//簽章欄
row = (XSSFRow)sheet.GetRow(27);
row.Cells[0].SetCellValue(row.Cells[0].StringCellValue.Replace("{user_name}", objDetail.user.user_name));
//匯款項目與發票
int payment_count = objDetail.payments.Count;
int invoice_count = objDetail.invoices.Count;
int real_count = 0;
if (payment_count > invoice_count)
{
real_count = payment_count;
}
else {
real_count = invoice_count;
}
for (int i = 1; i <= real_count; i++) {
int rowIndex = 20 + i;
CopyRow((XSSFWorkbook)workbook, (XSSFSheet)sheet, rowIndex, rowIndex + 1);
if (i <= objDetail.payments.Count) {
//付款方式
row = (XSSFRow)sheet.GetRow(rowIndex);
string paymentStr = objDetail.payments[i - 1].payment_methodname;
if (objDetail.payments[i - 1].payment_descript != "") {
paymentStr += "; " + objDetail.payments[i - 1].payment_descript;
}
row.Cells[0].SetCellValue(paymentStr);
}
if (i <= objDetail.invoices.Count) {
//發票
row = (XSSFRow)sheet.GetRow(rowIndex);
row.Cells[3].SetCellValue(objDetail.invoices[i - 1].invoice_name);
row.Cells[4].SetCellValue(objDetail.invoices[i - 1].invoice_year.ToString() + "/" + objDetail.invoices[i - 1].invoice_month.ToString().PadLeft(2, '0'));
row.Cells[6].SetCellValue("$NT" + objDetail.invoices[i - 1].invoice_noTaxMoney.ToString("###,###"));
}
}
MemoryStream ms = new MemoryStream(); MemoryStream ms = new MemoryStream();
workbook.Write(ms); workbook.Write(ms);
ms.Flush(); ms.Flush();
@ -180,6 +227,111 @@ namespace QuotationMaker.Controllers
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
} }
///
/// HSSFRow Copy Command
///
/// Description: Inserts a existing row into a new row, will automatically push down
/// any existing rows. Copy is done cell by cell and supports, and the
/// command tries to copy all properties available (style, merged cells, values, etc...)
///
private void CopyRow(XSSFWorkbook workbook, XSSFSheet worksheet, int sourceRowNum, int destinationRowNum)
{
// Get the source / new row
XSSFRow newRow = (XSSFRow)worksheet.GetRow(destinationRowNum);
XSSFRow sourceRow = (XSSFRow)worksheet.GetRow(sourceRowNum);
// If the row exist in destination, push down all rows by 1 else create a new row
if (newRow != null)
{
worksheet.ShiftRows(destinationRowNum, worksheet.LastRowNum, 1);
newRow = (XSSFRow)worksheet.CreateRow(destinationRowNum);
newRow.Height = sourceRow.Height;
}
else
{
newRow = (XSSFRow)worksheet.CreateRow(destinationRowNum);
}
// Loop through source columns to add to new row
for (int i = 0; i < sourceRow.LastCellNum; i++)
{
// Grab a copy of the old/new cell
XSSFCell oldCell = (XSSFCell)sourceRow.GetCell(i);
XSSFCell newCell = (XSSFCell)newRow.CreateCell(i);
// If the old cell is null jump to next cell
if (oldCell == null)
{
newCell = null;
continue;
}
// Copy style from old cell and apply to new cell
XSSFCellStyle newCellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
newCellStyle.CloneStyleFrom(oldCell.CellStyle); ;
newCell.CellStyle = newCellStyle;
// If there is a cell comment, copy
if (newCell.CellComment != null) newCell.CellComment = oldCell.CellComment;
// If there is a cell hyperlink, copy
if (oldCell.Hyperlink != null) newCell.Hyperlink = oldCell.Hyperlink;
// Set the cell data type
newCell.SetCellType(oldCell.CellType);
// Set the cell data value
switch (oldCell.CellType)
{
case CellType.Blank:
newCell.SetCellValue(oldCell.StringCellValue);
break;
case CellType.Boolean:
newCell.SetCellValue(oldCell.BooleanCellValue);
break;
case CellType.Error:
newCell.SetCellErrorValue(oldCell.ErrorCellValue);
break;
case CellType.Formula:
newCell.SetCellFormula(oldCell.CellFormula);
break;
case CellType.Numeric:
newCell.SetCellValue(oldCell.NumericCellValue);
break;
case CellType.String:
newCell.SetCellValue(oldCell.RichStringCellValue);
break;
case CellType.Unknown:
newCell.SetCellValue(oldCell.StringCellValue);
break;
}
}
// If there are are any merged regions in the source row, copy to new row
for (int i = 0; i < worksheet.NumMergedRegions; i++)
{
CellRangeAddress cellRangeAddress = worksheet.GetMergedRegion(i);
if (cellRangeAddress.FirstRow == sourceRow.RowNum)
{
CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.RowNum,
(newRow.RowNum +
(cellRangeAddress.FirstRow -
cellRangeAddress.LastRow)),
cellRangeAddress.FirstColumn,
cellRangeAddress.LastColumn);
worksheet.AddMergedRegion(newCellRangeAddress);
}
}
}
[Route("saveas")] [Route("saveas")]
public ActionResult SaveAs(IFormCollection obj) { public ActionResult SaveAs(IFormCollection obj) {
saveasResult ret = new saveasResult(); saveasResult ret = new saveasResult();

View File

@ -415,3 +415,5 @@ public class ChtNumConverter
return (negtive ? "負" : string.Empty) + t; return (negtive ? "負" : string.Empty) + t;
} }
} }

Binary file not shown.

Binary file not shown.