c# - 如何在 CsvLayout 中左对齐
问题描述
我发现csv格式的消息是一个数字,格式会右对齐。
此外,如果消息包含非数字,格式将左对齐。
有没有办法统一格式。
例如我的 excel 截图
以下是我的 NLog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<time type="AccurateLocal" />
<variable name="LogExpectedValue" value="" />
<variable name="LogActualValue" value="" />
<targets async="true">
<target name="ReportFile" xsi:type="File" fileName="Report/${logger}_${date:format=yyyy_MM_dd_HH}.csv" ncoding="utf-8"
archiveAboveSize="50000000"
archiveNumbering="Sequence"
keepFileOpen="false"
autoFlush="true"
maxArchiveFiles="3">
<layout xsi:type="CsvLayout" withheader="true">
<column name="ActualValue" layout="${var:LogActualValue:alignmentOnTruncation=left}" />
<column name="ExpectedValue" layout="${var:LogExpectedValue:alignmentOnTruncation=left}" />
</layout>
</target>
</targets>
<rules>
<logger name="Report" levels="Trace" writeTo="ReportFile" />
</rules>
</nlog>
解决方案
可以使用 excel 函数包装 CSV 数据,这很混乱,在这种情况下您需要更改分隔符。
此 excel 函数将数字更改为文本并用前导零填充以使其成为 6 位数字。
=TEXT(123,"00000") 其中 123 是您的数据。
因此,当您导出数据时,您将需要这样的东西(取决于您的语言)
string sOutput = "=TEXT(" & [你的数据] & "," & chr(34) & "00000" & chr(34) & ")"
尝试使用不带引号的制表符分隔文件。
如果可能的话,一个更简单的答案是在您的号码前加上一个文本字符。
推荐阅读
- c# - 无法使用图形 sdk 在在线共享点中创建新列表
- sql - 如何优化具有多个 OR、AND、IN 语句的查询?
- react-native - 可点击的地图视图标记
- ios - 当我向 iOS 请求许可时,我可以更改措辞并重新定义按钮吗?
- database - 查找一个字段的值不为空且不存在于其他字段中的 MongoDB 文档
- c - 为 FFTW 函数添加自定义后缀
- python - 在opencv中使用MSER检测灰度图像上的白色斑点
- shopify - 是否可以向 Shopify 中的所有产品批量添加元字段?
- javascript - 来自服务器的 React-Native 动态数组
- python - 如何将文件传输到转换器功能