将XLSX读入MFC应用程序

时间:2016-02-09 07:21:36

标签: c++ excel csv mfc xlsx

我需要将XLSX文档中的数据读入我的MFC应用程序。 它一直运作良好,但我现在要求阅读外语字符,他们迷路了。

我需要阅读的示例值是:

Лицеитяло

Мамаибебе

目前我首先使用我在网上找到的以下脚本将xlsx转换为csv

if WScript.Arguments.Count < 2 Then
    WScript.Echo \"Error!Please specify the source path and the destination.Usage: XlsToCsv SourcePath.xls Destination.csv
    Wscript.Quit
End If
Dim numRows
Dim numCols
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
oExcel.DisplayAlerts = False
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
Set oWorksheet = oBook.Worksheets(1)
oWorksheet.Activate
oWorksheet.Cells.Replace ",", "" 
Dim celltxt
celltxt = oWorksheet.Cells(1,1).Text
If InStr(1, celltxt, \"Date\") Then
    For count = 13 to 17
        oWorksheet.Columns(count).NumberFormat = "0"
    Next
Else
    For count = 1 to 4
        oWorksheet.Columns(count).NumberFormat = "0"
    Next
End If
numRows = oWorksheet.UsedRange.Rows.Count
numCols = oWorksheet.UsedRange.Columns.Count
Const adTypeText = 2
Const adSaveCreateOverWrite = 2
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
BinaryStream.Charset = "UTF-8"
BinaryStream.Type = adTypeText
BinaryStream.Open
For r = 1 To numRows
    s = ""
    For c = 1 To numCols
        s = s & oWorksheet.Cells(r, c).Value
        If c < numCols Then
            s = s & ","
        End If
    Next
BinaryStream.WriteText s, 1
Next
BinaryStream.SaveToFile WScript.Arguments.Item(1), adSaveCreateOverWrite
BinaryStream.Close
oBook.Close False
oExcel.Quit

一旦这个脚本运行并在进行任何进一步处理之前打开生成的csv,csv中的数据看起来很好。

一旦这个csv由aplication处理(使用CRecordSet加载),上面的值如下所示:

|Ы|¬TЖ|| ||TВTП|¬| -

|Ь|-| -..- || |-||| - ||

我发现如果在处理之前打开csv并转到“另存为”,则“另存为类型”值将显示在“Unicode文本”上。 如果我现在通过保存为“CSV(逗号分隔)”覆盖csv,然后让文件处理进入我的应用程序,结果值如下所示:

TшЎхшЄыю

|рьршсхсх

如果我手动将csv保存为“CSV(MS-DOS)”,然后让我的应用程序处理它,则值正确显示:

Лицеитяло

Мамаибебе

所以一方面,我不明白差异以及为什么我需要MS-DOS csv版本让我的应用程序正确读取值,其次,我需要能够正确处理数据而不需要hte手动保存在中间。

任何帮助都非常感谢!

非常感谢

更新:

只是为了测试,我更改了我的代码(运行脚本转换为csv)输出到.txt而不是.csv,输出文件再次看起来就在我的应用程序读入之前但它仍然从那里导入进入我的申请错误。

这是输出.txt内容:

From Date,To Date,Store ID,Store Name,Retailer,Retail Format,Region,Cluster,Market Attr 1 (Numeric),Market Attr 2 (Numeric),Market Attr 3 (Text),Market Attr 4 (Text),Market Attr 5 (Text),Product ID,EAN (Barcode),UPC (Product Code),Brand,Description,Size & Uom,Size,Uom,Supplier,Shrink Pack,Minimum Display Depth,KVI,Status,Product Height,Product Width,Product Depth,Supergroup A,Supergroup B,Category,Sub Category,Segment,Sub Segment,Product Attr 1 (Numeric),Product Attr 2 (Numeric),Product Attr 3 (Text),Product Attr 4 (Text),Product Attr 5 (Text),Sales (Value),Units (Volume),Sales at Cost,Ranging Indicator,Fact 1 (Numeric),Fact 2 (Numeric),Fact 3 (Numeric),Fact 4 (Text),Fact 5 (Text)
01/01/2014,31/01/2016,130,Maritza,Clicks,Pharmacy,Plovdiv,A Range,1.99,2.99,Attribute 3,Attribute 4,Attribute 5,1347,7612729000518,ПОЛИНЕЙЛ лечебен лак за нокти 80мг/гр 33мл,Coca Cola,ПОЛИНЕЙЛ лечебен лак за нокти 80мг/гр 33мл,330ml,330,ml,Coca Cola,6,2,1,Active,CONVERT,37,36,Лице и тяло,Медицинска козметика,Carbonated Soft Drinks,Diet,On The Go,Cans,1.99,2.99,Attribute 3,Attribute 4,Attribute 5,136.27,12,122.21,1,1.99,2.99,3.99,Yes,No
01/01/2014,31/01/2016,130,Maritza,,Pharmacy,Plovdiv,,,,,,,1349,5903263245933,СУДО крем антисептичен 125гр,,СУДО крем антисептичен 125гр,,,,,,,,,66,6,6,Мама и бебе,Козметика бебе,,,,,,,,,,,,,,,,,,
01/01/2014,31/01/2016,130,Maritza,,Pharmacy,Plovdiv,,,,,,,1381,,СПРИНЦОВКА 1МЛ ТРИСЪСТАВНА инсулинова спринцовка х 1бр,,СПРИНЦОВКА 1МЛ ТРИСЪСТАВНА инсулинова спринцовка х 1бр,,,,,,,,,198,3,1,Мама и бебе,Храни и напитки бебе,,,,,,,,,,,,,,,,,,
01/01/2014,31/01/2016,130,Maritza,,Pharmacy,Plovdiv,,,,,,,1607,8716200646536,ФРИЗОПЕП AC 400гр,,ФРИЗОПЕП AC 400гр,,,,,,,,,123,10,10,Мама и бебе,Храни и напитки бебе,,,,,,,,,,,,,,,,,,

0 个答案:

没有答案