Click here to Skip to main content
15,915,019 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am trying to save information from my application to an Excel (.xls) spreadsheets. I would like to save them according to their fields and rows.

I have tried to code something like this but it's not saving the values in the correct columns. I have tried this code below:

objWriter.Writeln("{0,1}{1,30}{2,24{3,30}","Order_Number","Order_Quantity","Order_Weight","Time_Scanned")

For i = 0 To alsTxt1.count - 1
objWriter.Writeline(alsTxt1(i) + "  " + alsTxt2(i) + "  " + alsTxt3(i) + "  " + alsTxt4(i))
Next
objWriter.closer ()


Thanks
Posted
Updated 16-May-11 23:54pm
v2
Comments
Sergey Alexandrovich Kryukov 17-May-11 17:32pm    
Gibberish.
--SA

Your code doesn't make sense. You are definitely NOT writing an Excel file with this code.

It looks like you're writing a text file, but the format string in the first line of your code doesn't make sense.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-May-11 17:31pm    
Ha-ha. My 5.
--SA
I hope this code help you. First create the object and specify the headers.
VB
Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object

VB
oExcel = CreateObject("Excel.Application")
       oBook = oExcel.Workbooks.Add
       oSheet = oBook.Worksheets(1)
       lblstatus.Text = "Formatting Excel File..."
       lblstatus.Refresh()
       oSheet.Range("A2").Value = "Dept"
       oSheet.Columns(1).columnwidth = 3.71
       oSheet.Range("B2").Value = "Discipline"
       oSheet.Columns(2).columnwidth = 5.86
       oSheet.Columns(3).columnwidth = 5.0
       oSheet.Range("D2").Value = "Description"
       oSheet.Columns(4).columnwidth = 26.57
       oSheet.Range("E1").Value = "# of"
       oSheet.Columns(5).columnwidth = 6.29


Then you increase the row number each time you read a row in database or array, like this:
VB
oSheet.Range("A" + celda).Value = mnumdept
           oSheet.Range("B" + celda).Value = msubj_code
           oSheet.Range("C" + celda).Value = mcrse_numb
           oSheet.Range("D" + celda).Value = mtitle
           oSheet.Range("E" + celda).Value = mseq_num
           oSheet.Range("F" + celda).Value = menrol

numero=numero+1
celda=convert.tostring(numero)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900