我可以使用java格式化要写入CSV文件的数据吗

发布于 2024-09-25 20:57:28 字数 65 浏览 0 评论 0原文

我有一些代码将数据写入 CSV 文件,但它会将数据写入 CSV 没有正确格式化它。我想将某些特定文本加粗。这可能吗?

I have some code to write data into a CSV file, but it writes data into a CSV
without formatting it properly. I want to bold some specific text. Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无法言说的痛 2024-10-02 20:57:28

CSV 只是一种纯文本格式,因此您无法进行任何格式化。

如果您想要格式化,请考虑使用 Excel 库,例如 Apache POIJasper 报告。 (当然,然后您最终会得到一个 excel 文件而不是 CSV,因此根据您的情况可能合适也可能不合适)

作为旁注,编写 CSV 有一些奇怪的细微差别(例如确保引号,逗号等已正确转义)。我使用过一个不错的轻量级库,名为 Open CSV,如果您选择坚持使用,它可能会让您的生活更轻松使用普通的旧 CSV。

CSV is just a plain text format, so you can't do any formatting.

If you want formatting, consider using an Excel library such as Apache POI or Jasper Reports. (Of course, then you end up with an excel file rather than a CSV, so depending on your situation that may or may not be appropriate)

As a side note, there are some strange nuances to writing CSV (such as making sure quotes, commas etc are properly escaped). There's a nice lightweight library I've used called Open CSV that might make your life easier if you choose to just stick with plain old CSV.

如梦亦如幻 2024-10-02 20:57:28

CSV是纯文本文件格式,不能使用任何文本效果。

CSV is a plain text file format, you can not use any text effect.

月棠 2024-10-02 20:57:28

据我所知,CSV 是一种纯文本格式。
如果您要创建 csv,以便可以在 Excel 中打开它,那么我建议您查看 MS Excel XML 格式。

http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats#Excel_XML_Spreadsheet_example

一个示例是如下(取自维基百科链接,这使得一些文本变为粗体)

   <?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook
   xmlns="urn:schemas-microsoft-com:office:spreadsheet"
   xmlns:o="urn:schemas-microsoft-com:office:office"
   xmlns:x="urn:schemas-microsoft-com:office:excel"
   xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
   xmlns:html="http://www.w3.org/TR/REC-html40">
  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
    <Author>Darl McBride</Author>
    <LastAuthor>Bill Gates</LastAuthor>
    <Created>2007-03-15T23:04:04Z</Created>
    <Company>SCO Group, Inc.</Company>
    <Version>11.8036</Version>
  </DocumentProperties>
  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
    <WindowHeight>6795</WindowHeight>
    <WindowWidth>8460</WindowWidth>
    <WindowTopX>120</WindowTopX>
    <WindowTopY>15</WindowTopY>
    <ProtectStructure>False</ProtectStructure>
    <ProtectWindows>False</ProtectWindows>
  </ExcelWorkbook>
  <Styles>
    <Style ss:ID="Default" ss:Name="Normal">
      <Alignment ss:Vertical="Bottom" />
      <Borders />
      <Font />
      <Interior />
      <NumberFormat />
      <Protection />
    </Style>
    <Style ss:ID="s21">
      <Font x:Family="Swiss" ss:Bold="1" />
    </Style>
  </Styles>
  <Worksheet ss:Name="Sheet1">
    <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="5"
       x:FullColumns="1" x:FullRows="1">
      <Row>
        <Cell>
          <Data ss:Type="String">Text in cell A1</Data>
        </Cell>
      </Row>
      <Row>
        <Cell ss:StyleID="s21">
          <Data ss:Type="String">Bold text in A2</Data>
        </Cell>
      </Row>
      <Row ss:Index="4">
        <Cell ss:Index="2">
          <Data ss:Type="Number">43</Data>
        </Cell>
      </Row>
      <Row>
        <Cell ss:Index="2" ss:Formula="=R[-1]C/2">
          <Data ss:Type="Number">21.5</Data>
        </Cell>
      </Row>
    </Table>
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
      <Print>
        <ValidPrinterInfo />
        <HorizontalResolution>600</HorizontalResolution>
        <VerticalResolution>600</VerticalResolution>
      </Print>
      <Selected />
      <Panes>
        <Pane>
          <Number>3</Number>
          <ActiveRow>5</ActiveRow>
          <ActiveCol>1</ActiveCol>
        </Pane>
      </Panes>
      <ProtectObjects>False</ProtectObjects>
      <ProtectScenarios>False</ProtectScenarios>
    </WorksheetOptions>
  </Worksheet>
</Workbook

Not that I am aware of, CSV is a plain text format.
If you are creating a csv, so that you can open it up in Excel, then I would suggest taking a look at the MS Excel XML format.

http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats#Excel_XML_Spreadsheet_example

An example would be as follows (taken from the wikipedia link, and this makes some text BOLD)

   <?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook
   xmlns="urn:schemas-microsoft-com:office:spreadsheet"
   xmlns:o="urn:schemas-microsoft-com:office:office"
   xmlns:x="urn:schemas-microsoft-com:office:excel"
   xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
   xmlns:html="http://www.w3.org/TR/REC-html40">
  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
    <Author>Darl McBride</Author>
    <LastAuthor>Bill Gates</LastAuthor>
    <Created>2007-03-15T23:04:04Z</Created>
    <Company>SCO Group, Inc.</Company>
    <Version>11.8036</Version>
  </DocumentProperties>
  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
    <WindowHeight>6795</WindowHeight>
    <WindowWidth>8460</WindowWidth>
    <WindowTopX>120</WindowTopX>
    <WindowTopY>15</WindowTopY>
    <ProtectStructure>False</ProtectStructure>
    <ProtectWindows>False</ProtectWindows>
  </ExcelWorkbook>
  <Styles>
    <Style ss:ID="Default" ss:Name="Normal">
      <Alignment ss:Vertical="Bottom" />
      <Borders />
      <Font />
      <Interior />
      <NumberFormat />
      <Protection />
    </Style>
    <Style ss:ID="s21">
      <Font x:Family="Swiss" ss:Bold="1" />
    </Style>
  </Styles>
  <Worksheet ss:Name="Sheet1">
    <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="5"
       x:FullColumns="1" x:FullRows="1">
      <Row>
        <Cell>
          <Data ss:Type="String">Text in cell A1</Data>
        </Cell>
      </Row>
      <Row>
        <Cell ss:StyleID="s21">
          <Data ss:Type="String">Bold text in A2</Data>
        </Cell>
      </Row>
      <Row ss:Index="4">
        <Cell ss:Index="2">
          <Data ss:Type="Number">43</Data>
        </Cell>
      </Row>
      <Row>
        <Cell ss:Index="2" ss:Formula="=R[-1]C/2">
          <Data ss:Type="Number">21.5</Data>
        </Cell>
      </Row>
    </Table>
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
      <Print>
        <ValidPrinterInfo />
        <HorizontalResolution>600</HorizontalResolution>
        <VerticalResolution>600</VerticalResolution>
      </Print>
      <Selected />
      <Panes>
        <Pane>
          <Number>3</Number>
          <ActiveRow>5</ActiveRow>
          <ActiveCol>1</ActiveCol>
        </Pane>
      </Panes>
      <ProtectObjects>False</ProtectObjects>
      <ProtectScenarios>False</ProtectScenarios>
    </WorksheetOptions>
  </Worksheet>
</Workbook
紫竹語嫣☆ 2024-10-02 20:57:28

我是普通的旧常规 CSV 编号。但是,您没有理由不能在写出数据之前对其进行编码...例如,粗体的 HTML 标记是 。这将向您准确表明文本的哪些部分是粗体的,并且来自众所周知的标准仍然是人类可读的。主要缺点是您必须在读取数据后对其进行解析:(

还有其他需要考虑的事情,既然您正在写出数据,为什么不以 RTF 或其他支持粗体等的格式将其写为逗号分隔值?通常CSV 是纯文本,但您没有理由不能以其他方式写出它,只需记住以相同的格式读回它即可。

I plain old regular CSV no. But there is no reason why you could not encode your data before writing it out... for example the HTML tags for bold is <b></b>. This would signal to you exactly which portions of the text are bold and being a from a well know standard is still human readable too. The main drawback is you have to parse your data after you read it :(

Something else to consider, since you are writing the data out why not write it out as comma separated values in RTF or some other format that does support bold etc? Normally CSV is plain text but there is no reason why you couldn't write it out another way. Just remember to read it back in the same format...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文