简体   繁体   English

Java存储号在Excel中为String

[英]Java store number as String in excel

Good day, 美好的一天,

I have a java web application which allow user to download some data in csv format. 我有一个Java Web应用程序,允许用户下载csv格式的一些数据。

Everything is fine unless I have numeric data but start with 0, for example 010 . 一切都很好,除非我有数字数据,但以0开头,例如010

I wish to store 010 correctly into the csv file, but it will auto change to 10 . 我希望将010正确存储到csv文件中,但是它将自动更改为10

In my java code, I tried to append ' in front of the number, but it will store to '010 instead of 010 在我的Java代码中,我尝试在数字前添加' ,但是它将存储到'010而不是010

data.alignCenter().text( "'010" ); \\ data is my TableBuilder object.

Kindly advise. 好心提醒。

As I understand you are looking to store the number in string format. 据我了解,您正在寻求以字符串格式存储数字。 In Excel I stored the number's 10 as string in excel by specifying '010 and saved the document as CSV. 在Excel中,我通过指定'010将数字10作为字符串存储在excel中,并将文档另存为CSV。 As soon as I saved the document to CSV, the formatting information got lost and the data got stored as 010 . 将文档保存为CSV格式后,格式信息立即丢失,数据存储为010

Once I open the CSV file in excel directly , the data gets auto changed to 10 . 一旦我直接在excel中打开CSV文件,数据就会自动更改为10 I did a test with a text file just specifying '10 but the when I see the data in excel it gets transformed to some wierd text. 我使用仅指定'10的文本文件进行了测试,但是当我在excel中看到数据时,它会转换为一些奇怪的文本。 So I do not have the leading ' character when saving numbers with leading 0's. 因此,在保存带0开头的数字时,我没有'字符。 SO, DO NOT OPEN THE CSV FILE DIRECTLY WITH EXCEL! 因此,请勿直接使用EXCEL直接打开CSV文件!

Here is a link that details how to keep leading zeroes in excel. 这是一个详细说明如何在excel中保持前导零的链接

Steps that will be useful borrowed from the page: 从页面借用的有用步骤:

  1. Open a new worksheet in Excel (see below for Excel screenshots.) 在Excel中打开一个新的工作表(请参阅下面的Excel屏幕截图。)
  2. Open the Data tab 打开数据标签
  3. Click on the From text button in the Get External Data section 单击获取外部数据部分中的来自文本按钮
  4. Select your CSV file to import 选择要导入的CSV文件
  5. Select the "Delimited" radio button -- Text Import Wizard, Step 1 determines that your data is delimited 选择“定界”单选按钮- 文本导入向导,步骤1确定您的数据已定界
  6. Click Next 点击下一步
  7. Check "Comma" as a delimiter (column dividers will appear in preview)-- Step 2 lets you set delimiters 选中“逗号”作为分隔符(列分隔符将出现在预览中)- 步骤2可让您设置分隔符
  8. Click Next 点击下一步
  9. Highlight the column(s) with leading zeros in Step 3 第3步中突出显示带有前导零的列
  10. Mark those columns format as "text" by clicking the radio button in the Column Data Format section . 通过单击“ 列数据格式”部分中的单选按钮,将这些列格式标记为“文本” NOTE: You will need to do this for each column where the data contains leading zeros.
  11. Click Finish 点击完成
  12. The leading zeros will still be there in the new worksheet with the imported data. 新的工作表中的前导零仍然会保留在导入的数据中。

I found the solution. 我找到了解决方案。

Instead of append ' in front of the number, I append =" in front, and " at behind. 代替在编号前面附加“的,我追加="前,和"在后面。

This solve my problem. 这解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM