问题描述
我正在调用Web服务,Web服务中的数据采用csv格式。
如果我尝试将数据保存在xls / xlsx中,那么在工作簿中可以获得多个工作表。
那么我怎么可以用c#中的多个tab / sheet将数据保存在csv中。
我知道具有多个选项卡的csv是不实用的,但是有没有使用多个选项卡/工作表将数据保存在csv中?
CSV,作为文件格式,假定一个表数据;在Excel中,这是一张工作簿。虽然这只是纯文本,您可以以任何方式解释,标准CSV格式不支持您的主管的想法。
你可以软化什么您需要几种方法:
-
对于每张工作表使用不同的文件,具有相关但不同的名称,如Book1_Sheet1 ,Book1_Sheet2等。然后,您可以在第一个下划线之前通过文本查找相关文件组。这是最简单的实现方式,但是要求用户在每个逻辑工作簿中分别绕过多个文件,如果在这个随机播放中丢失了这些文件,那么您就丢失了这些数据。
-
执行上述操作,并将文件zip到一个可以移动的单个存档中。您保留上述选项的纯CSV优势,以及让一个文件移动而不是几个的方便,但不得不压缩/解压缩归档以获取实际文件的缺点。为了缓解痛苦,如果您使用的是.NET 4.5,您可以访问内置的ZipFile实现,如果您不能使用开源的DotNetZip或SharpZipLib,任何一种都将允许您以编程方式创建和使用标准的Windows ZIP文件。您也可以使用几乎通用的.tar.gz(aka .tgz)组合,但您的用户将需要您的程序或第7方压缩工具,例如7Zip或WinRAR,以从一组导出的CSV中创建归档。 p>
-
实现准CSV格式,其中空行(仅包含换行符)充当制表符分隔符,您的解析器将期待一行新行头文件后面是新配置中的数据行。标准CSV的这种变体可能不被CSV的其他用户所读取,因为它不符合预期的文件格式,因此我建议您不要使用.csv扩展名,因为它会混淆和挫败用户期待能够在其他应用程序(如电子表格)中打开它。
I am calling a web service and the data from the web service is in csv format.If I try to save data in xls/xlsx, then I get multiple sheets in a workbook.So, how can I save the data in csv with multipletab/sheets in c#.
I know csv with multiple tabs is not practical, but is there any damn way or any library to save data in csv with multiple tabs/sheet?
CSV, as a file format, assumes one "table" of data; in Excel terms that's one sheet of a workbook. While it's just plain text, and you can interpret it any way you want, the "standard" CSV format does not support what your supervisor is thinking.
You can fudge what you want a couple of ways:
Use a different file for each sheet, with related but distinct names, like "Book1_Sheet1", "Book1_Sheet2" etc. You can then find groups of related files by the text before the first underscore. This is the easiest to implement, but requires users to schlep around multiple files per logical "workbook", and if one gets lost in the shuffle you've lost that data.
Do the above, and also "zip" the files into a single archive you can move around. You keep the pure CSV advantage of the above option, plus the convenience of having one file to move instead of several, but the downside of having to zip/unzip the archive to get to the actual files. To ease the pain, if you're in .NET 4.5 you have access to a built-in ZipFile implementation, and if you are not you can use the open-source DotNetZip or SharpZipLib, any of which will allow you to programmatically create and consume standard Windows ZIP files. You can also use the nearly universal .tar.gz (aka .tgz) combination, but your users will need either your program or a third-party compression tool like 7Zip or WinRAR to create the archive from a set of exported CSVs.
Implement a quasi-CSV format where a blank line (containing only a newline) acts as a "tab separator", and your parser would expect a new line of column headers followed by data rows in the new configuration. This variant of standard CSV may not readable by other consumers of CSVs as it doesn't adhere to the expected file format, and as such I would recommend you don't use the ".csv" extension as it will confuse and frustrate users expecting to be able to open it in other applications like spreadsheets.
这篇关于csv有多个选项卡/表可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!