B4J Question jPoi 5 - working with csv files

ilan

Expert
Licensed User
Longtime User
hi

i need to import some data from an excel file. the file extension is csv. using it with jPoi 5 gives me an error:


if i open the file in excel and save it as .xlsx it is working fine.
is it possible to make it run using csv files?

thanx
 

DonManfred

Expert
Licensed User
Longtime User
if i open the file in excel and save it as .xlsx it is working fine.
Excel has probably 100mb of code to import an CSV. ?

You need to build the Excel file by yourself based on the CSV-Data.

All needed methods to do it are available in xlutils

I don´t know whether there is a csv-import-method though
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User


B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private xl As XLUtils
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    xl.Initialize
    Dim parser As CSVParser
    parser.Initialize
    Dim tr As TextReader
    tr.Initialize2(File.OpenInput("C:\Users\H\Downloads\countries-20140629.csv", ""), "Windows-1252")
    Dim input As String = tr.ReadAll
    Dim table As List = parser.Parse(input, ",", False)
    CreateWorkbook(table, "C:\Users\H\Downloads\test.xlsx")
End Sub

Private Sub CreateWorkbook (Table As List, FileName As String)
    Dim wb As XLWorkbookWriter = xl.CreateWriterBlank
    Dim sheet As XLSheetWriter = wb.CreateSheetWriterByName("Sheet1")
    Dim address As XLAddress = xl.AddressName("A1")
    For Each row() As String In Table
        address.Col0Based = 0
        For Each cell As String In row
            sheet.PutString(address, cell)
            address.Col0Based = address.Col0Based + 1
        Next
        address.Row0Based = address.Row0Based + 1
    Next
    Dim TitleStyle As XLStyle = wb.CreateStyle.FontBold(12).ForegroundColor(xl.COLOR_GREY_25_PERCENT)
    sheet.AddStylesToRange(xl.CreateXLRange(xl.AddressName("A1"), xl.AddressName("C1")), Array(TitleStyle))
    sheet.AutoSizeColumn(0)
    sheet.AutoSizeColumn(1)
    sheet.AutoSizeColumn(2)
    wb.SaveAs(FileName, "", False)
End Sub

CSV: http://blog.plsoucy.com/2012/04/iso-3166-country-code-list-csv-sql/
CSVParser: https://www.b4x.com/android/forum/threads/b4x-csvparser-csv-parser-and-generator.110901/#content
 
Upvote 2
Cookies are required to use this site. You must accept them to continue using the site. Learn more…