ClosedX에서 통화 형식을 지정하는 방법ML(숫자)
ClosedX를 사용하고 있습니다.ML - 데이터 테이블 개체를 사용자에게 표시하기 위해 Excel 스프레드시트로 변환합니다.DataTable 개체는 (NHibernate에서) 모든 db 값을 문자열에 할당한 다음 아래와 같이 형식을 지정하여 간단히 빌드할 수 있습니다.
//formatting
EstimatedCost = Currency.SafeToString(Currency.Create(n.EstimatedCost)),
그런 다음 열 유형을 속성 유형으로 설정합니다.모든 경우 문자열입니다.
출력 Excel 시트에서 열이 통화로 설정되어 있지만 숫자가 텍스트 경고로 지정되어 있으면 올바르게 정렬되지 않습니다.
문제는 우리가 모든 데이터를 DataTable로 구축하기 때문에 ClosedX를 장식할 기회가 없다는 것입니다.ML 열이 정확합니다.제가 생각하지 않는 빠른 방법이 있을까요?
public const string ExcelDataType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
public static MemoryStream GenerateExcelFileFromData(IEnumerable<KeyValuePair<string, DataTable>> tabs, int colWidth = 100)
{
var workbook = new XLWorkbook { ColumnWidth = colWidth };
foreach (var enumerable in tabs)
{
workbook.Worksheets.Add(enumerable.Value, enumerable.Key);
}
...
public static DataTable ConvertToDataTable<T>(IEnumerable<T> varlist, List<string> excludedColumns, bool titleizeColumn = false)
{
var dtReturn = new DataTable();
// column names
PropertyInfo[] oProps = null;
if (varlist == null) return dtReturn;
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others will follow
if (oProps == null)
{
oProps = rec.GetType().GetProperties();
foreach (PropertyInfo pi in oProps)
{
if (excludedColumns.Contains(pi.Name))
{
continue;
}
var colType = pi.PropertyType;
dtReturn.Columns.Add(new DataColumn(GetColumnName(pi, titleizeColumn), colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (var pi in oProps.Where(pi => !excludedColumns.Contains(pi.Name)))
{
try
{
dr[GetColumnName(pi, titleizeColumn)] = pi.GetValue(rec, null) ?? DBNull.Value;
}
catch (ArgumentException)
{
dr[GetColumnName(pi, titleizeColumn)] = DBNull.Value;
}
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
통화 값의 형식은 다음과 같습니다.
worksheet.Cell(rowIndex, columnIndex).Style.NumberFormat.Format = "$0.00";
worksheet.Cell(rowIndex, columnIndex).DataType = XLCellValues.Number; // Use XLDataType.Number in 2018 and after
통화로 알고 있는 모든 열을 얻고 숫자를 설정할 수 있습니다.포맷. 포맷.아래는 어떻게 수행될 수 있는지에 대한 샘플입니다.미국 달러 통화에 사용할 올바른 형식은 다음과 같습니다."[$$-en-US]#,##0.00_);[Red]([$$-en-US]#,##0.00)"
이 형식을 사용할 때 Excel에서 스프레드시트를 열고 셀 형식으로 이동하면 통화로 설정된 것을 볼 수 있습니다.
// Assume all columns that have "price" in the heading are currency types and format as currency.
var priceColumns = table.Columns(c => c.FirstCell().Value.ToString().Contains("price", StringComparison.InvariantCultureIgnoreCase));
foreach (var priceColumn in priceColumns)
{
try
{
// Get all the cells in this column, except for the header row
var cells = priceColumn.Cells(2, rowCount);
cells.Style.NumberFormat.Format = @"[$$-en-US]#,##0.00_);[Red]([$$-en-US]#,##0.00)";
}
catch { } // Exception here means not all of the cells have a numeric value.
}
형식을 올바르게 설정하는 한 가지 쉬운 방법은 Excel로 이동하여 셀을 원하는 형식을 선택한 다음 셀을 마우스 오른쪽 단추로 클릭하고 셀 형식을 선택하면 선택한 형식에서 형식 코드를 복사할 수 있습니다.
// I had to escape double quotes though
string myFormat = "\"$\"#,##0;[RED]-\"$\"#,##0";
worksheet.Cell("A1").Style.NumberFormat.Format = myFormat;
var currency = "$";
worksheet.Cell(rowIndex, columnIndex)
.SetDataType(XLDataType.Number)
.Style.NumberFormat.SetFormat($"{currency}0.00");
ws.Cell(ro, co).Style.NumberFormat.Format = "[$$-en-US] #,##0.00";
ws.Cell(ro, co).DataType = XLDataType.Number;
언급URL : https://stackoverflow.com/questions/30380218/how-to-format-currency-in-closedxml-as-numeric
'programing' 카테고리의 다른 글
bash -c'는 무엇을 합니까? (0) | 2023.04.29 |
---|---|
클라이언트 사이드 JavaScript를 사용하여 몇 가지 스타일로 Excel 파일을 만듭니다(가능한 경우 js-xlsx 라이브러리를 사용하여). (0) | 2023.04.29 |
Tomcat을 사용하여 Eclipse에서 웹 서비스를 실행하는 동일한 경로 오류가 있는 여러 컨텍스트 (0) | 2023.04.29 |
코코아 응용 프로그램의 인포리스트에서 "번들 표시 이름"과 "번들 이름"의 차이점은 무엇입니까? (0) | 2023.04.29 |
파일의 두 번째 열을 기준으로 데이터 정렬 (0) | 2023.04.24 |