java 我可以使用java格式化要写入CSV文件的数据吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3839453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I format data that is to be written in CSV file using java
提问by Tokendra Kumar Sahu
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?
我有一些代码可以将数据写入 CSV 文件,但它会将数据写入 CSV 文件而没有正确格式化。我想加粗一些特定的文字。那可能吗?
回答by Michael D
CSV is just a plain text format, so you can't do any formatting.
CSV 只是一种纯文本格式,因此您无法进行任何格式化。
If you want formatting, consider using an Excel library such as Apache POIor 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)
如果您想要格式化,请考虑使用 Excel 库,例如Apache POI或Jasper Reports。(当然,那么你最终会得到一个 excel 文件而不是一个 CSV,所以取决于你的情况可能合适也可能不合适)
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 CSVthat might make your life easier if you choose to just stick with plain old CSV.
作为旁注,编写 CSV 有一些奇怪的细微差别(例如确保引号、逗号等正确转义)。我使用了一个很好的轻量级库,称为Open CSV,如果您选择坚持使用普通的旧 CSV,它可能会让您的生活更轻松。
回答by Giuseppe Cardone
CSV is a plain text file format, you can not use any text effect.
CSV 是纯文本文件格式,不能使用任何文字效果。
回答by Codemwnci
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.
我不知道,CSV 是纯文本格式。如果您正在创建一个 csv,以便您可以在 Excel 中打开它,那么我建议您查看 MS Excel XML 格式。
http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats#Excel_XML_Spreadsheet_example
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
回答by BigMac66
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 :(
我普通的普通 CSV 没有。但是没有理由在写出数据之前不能对数据进行编码……例如,粗体的 HTML 标记是 <b></b>。这将准确地向您发出信号,文本的哪些部分是粗体的,并且来自众所周知的标准仍然是人类可读的。主要缺点是您必须在阅读数据后对其进行解析:(
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...
还有一些需要考虑的事情,既然你正在写数据,为什么不把它写成 RTF 中的逗号分隔值或支持粗体等的其他格式?通常 CSV 是纯文本,但没有理由不能用另一种方式写出来。请记住以相同的格式阅读它......

