首页 > 解决方案 > 如何在 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>

标签: c#nlog

解决方案


可以使用 excel 函数包装 CSV 数据,这很混乱,在这种情况下您需要更改分隔符。

此 excel 函数将数字更改为文本并用前导零填充以使其成为 6 位数字。

=TEXT(123,"00000") 其中 123 是您的数据。

因此,当您导出数据时,您将需要这样的东西(取决于您的语言)

string sOutput = "=TEXT(" & [你的数据] & "," & chr(34) & "00000" & chr(34) & ")"

尝试使用不带引号的制表符分隔文件。

如果可能的话,一个更简单的答案是在您的号码前加上一个文本字符。


推荐阅读