fixed table and excel

master
嘉祥 詹 2024-10-09 17:48:25 +08:00
parent c0e9cf4748
commit 5f49b51dd1
5 changed files with 184 additions and 11 deletions

View File

@ -207,6 +207,7 @@ namespace QuotationMaker.Controllers
row = (XSSFRow)sheet.GetRow(16);
row.Cells[0].SetCellValue(row.Cells[0].StringCellValue.Replace("{quotation_sa}", services_aggrement));
//付款方式與發票
string quotation_grandTotal = ChtNumConverter.ToChtNum((long)objDetail.quotation_grandTotal);
@ -287,6 +288,9 @@ namespace QuotationMaker.Controllers
//優惠總價為置
int specTotalIndex = 12;
//服務協議位置
int saIndex = 16;
//主項目編號
int itemNumber = 0;
//開始產生項目價格列
@ -306,6 +310,7 @@ namespace QuotationMaker.Controllers
sutTotalAcIndex++;
specTotalIndex++;
saRowIndex++;
saIndex++;
foreach (quotationSubItem objSub in objMain.quotationSubItems) {
CopyRow((XSSFWorkbook)workbook, (XSSFSheet)sheet, editRowIndex, editRowIndex + 1);
@ -316,12 +321,13 @@ namespace QuotationMaker.Controllers
sutTotalAcIndex++;
specTotalIndex ++;
saRowIndex++;
saIndex++;
rowCount++;
}
//項目編號合併並填入編號
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(editIndex, editIndex + rowCount, 0, 0));
//sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(editIndex, editIndex + rowCount, 0, 0));
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(editIndex, editIndex, 1, 6));
row = (XSSFRow)sheet.GetRow(editIndex);
@ -330,9 +336,14 @@ namespace QuotationMaker.Controllers
int acTotal = 0;
int subItemNum = 0;
//合併子項目row
foreach (quotationSubItem objSub in objMain.quotationSubItems)
{
subItemNum++;
string subItemNumberTxt = itemNumber.ToString() + "-" + subItemNum.ToString();
string descript = objSub.quotationSubItem_descript;
if (objSub.quotationSubItem_hasAC == "N" && objMain.quotationMainItem_ac > 0)
@ -343,6 +354,7 @@ namespace QuotationMaker.Controllers
editIndex++;
row = (XSSFRow)sheet.GetRow(editIndex);
row.Height = -1;
row.Cells[0].SetCellValue(subItemNumberTxt);
row.Cells[1].SetCellValue(objSub.quotationSubItem_name);
row.Cells[2].SetCellValue(descript);
@ -411,6 +423,7 @@ namespace QuotationMaker.Controllers
sutTotalAcIndex+=3;
specTotalIndex+=3;
saRowIndex += 3;
saIndex +=3;
//複製小計欄
CopyRow((XSSFWorkbook)workbook, (XSSFSheet)sheet, subTotalIndex, editIndex);
@ -425,6 +438,7 @@ namespace QuotationMaker.Controllers
editIndex++;
editRowIndex++;
//複製AC欄
CopyRow((XSSFWorkbook)workbook, (XSSFSheet)sheet, acIndex, editIndex);
@ -439,6 +453,14 @@ namespace QuotationMaker.Controllers
editIndex++;
editRowIndex++;
if (objMain.quotationMainItem_ac <= 0) {
row.ZeroHeight = true;
}
//複製此項目總金額欄
CopyRow((XSSFWorkbook)workbook, (XSSFSheet)sheet, sutTotalAcIndex, editIndex);
row = (XSSFRow)sheet.GetRow(editIndex);
@ -496,14 +518,101 @@ namespace QuotationMaker.Controllers
saRowIndex--;
}
row = (XSSFRow)sheet.GetRow(saRowIndex);
row.Height = 8190;
row = (XSSFRow)sheet.GetRow(saRowIndex + 1);
row.Height = 8190;
//自适应高度9代表从第10行开始表头部分不会多行endRow是结束的行也就是有数据内容的行数毕竟没内容的单元格也没必要自适应行高
for (int i = saRowIndex; i < saRowIndex + 3; i++)
{
if (sheet.GetRow(i) == null)
{
continue;
}
IRow ICurRow = sheet.GetRow(i);
int OldHg = ICurRow.Height;//当前行高
int newHg = ICurRow.Height;//新行高,默认与当前行高一致
row = (XSSFRow)sheet.GetRow(saRowIndex + 2);
row.Height = 8000;
for (int j = 0; j < 1; j++) {
if (ICurRow.GetCell(j) == null)
{
continue;
}
ICell CurCell = ICurRow.GetCell(j);
double cellWidth = 0;
for (int k = 0; k < 7; k++) {
cellWidth += sheet.GetColumnWidth(k);
}
int cellStringLength = GetCellStringLength(CurCell.StringCellValue);
if (cellStringLength > cellWidth) {
double newHg1 = (double)(OldHg * (cellStringLength / cellWidth + 1));
double lastHg = newHg1;
if (lastHg > 8190) {
row = (XSSFRow)sheet.GetRow(saRowIndex);
row.Height = 8190;
lastHg = lastHg - 8190;
}
if (lastHg > 8190)
{
row = (XSSFRow)sheet.GetRow(saRowIndex + 1);
row.Height = 8190;
lastHg = lastHg - 8190;
}
else {
row = (XSSFRow)sheet.GetRow(saRowIndex + 1);
row.Height = (short)lastHg;
lastHg = lastHg - 8190;
}
if (lastHg > 0) {
row = (XSSFRow)sheet.GetRow(saRowIndex + 2);
if (lastHg > 8190)
{
row.Height = 8190;
}
else
{
row.Height = (short)lastHg;
}
}
}
}
}
//sheet.AutoSizeRow(saRowIndex);
//sheet.AutoSizeRow(saRowIndex + 1);
//sheet.AutoSizeRow(saRowIndex + 2);
//row = (XSSFRow)sheet.GetRow(saRowIndex);
//row.Height = 8190;
//row = (XSSFRow)sheet.GetRow(saRowIndex + 1);
//row.Height = 8190;
//row = (XSSFRow)sheet.GetRow(saRowIndex + 2);
//row.Height = 8000;
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
@ -531,8 +640,38 @@ namespace QuotationMaker.Controllers
return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8");
}
/// <summary>
/// 计算单元格内容长度
/// 用 ASCII 码范围判断字符是不是汉字
/// </summary>
/// <param name="text">待判断字符或字符串</param>
/// <returns>宽度</returns>
public int GetCellStringLength(string text)
{
//汉字 453、数字和字母 239
//这里的汉字与字母的宽度值要根据Excel的字体大小计算
//例如你单元格宽度1000能放10个汉字那汉字宽度就是1000/10=100
//同理计算字母宽度
int result = 0;
foreach (char t in text)
{
if (t == 10)
{
//忽略字符串中的换行符\n
continue;
}
else if (t > 127)
{
result += 660;
}
else
{
result += 370;
}
}
return result;
}
///

View File

@ -222,6 +222,10 @@ public static class GlobalClass
}
}
/// <summary>
/// 日期转换为中文大写
/// </summary>
@ -236,6 +240,9 @@ public class UpperConvert
//把数字转换为大写
public string numtoUpper(int num)
{
String str = num.ToString();
string rstr = "";
int n;
@ -259,6 +266,33 @@ public class UpperConvert
}
return rstr;
}
public string yeartoUpper(int number)
{
string[] chineseNumber = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
string[] unit = { "", "十", "百", "千", "萬", "十萬", "百萬", "千萬", "億", "十億", "百億", "千億", "兆", "十兆", "百兆", "千兆" };
StringBuilder ret = new StringBuilder();
string inputNumber = number.ToString();
int idx = inputNumber.Length;
bool needAppendZero = false;
foreach (char c in inputNumber)
{
idx--;
if (c > '0')
{
if (needAppendZero)
{
ret.Append(chineseNumber[0]);
needAppendZero = false;
}
ret.Append(chineseNumber[(int)(c - '0')] + unit[idx]);
}
else
needAppendZero = true;
}
return ret.Length == 0 ? chineseNumber[0] : ret.ToString();
}
//月转化为大写
public string monthtoUpper(int month)
{
@ -298,10 +332,10 @@ public class UpperConvert
//日期转换为大写
public string dateToUpper(System.DateTime date)
{
int year = date.Year;
int year = date.Year - 1911;
int month = date.Month;
int day = date.Day;
return numtoUpper(year) + "年" + monthtoUpper(month) + "月" + daytoUpper(day) + "日";
return yeartoUpper(year) + "年" + monthtoUpper(month) + "月" + daytoUpper(day) + "日";
}
}

View File

@ -182,7 +182,7 @@
<div class="col-md-12" id="project_name_div">
<div class="form-group">
<label for="project_name">專案名稱</label> <input type="text" id="project_name" class="form-control">
<label for="project_name">專案名稱</label> <input type="text" id="project_name" maxlength="60" class="form-control">
</div>
</div>
<div class="col-md-12" id="prm_project_serial">

Binary file not shown.

Binary file not shown.