Printen landscape

Status
Niet open voor verdere reacties.

Doofenshmirt

Gebruiker
Lid geworden
3 nov 2011
Berichten
222
Hoi,

gebruik deze code om wat op te slaan als xml bestand.
Weet iemand hoe ik deze code kan aanpassen dat hij wordt opgeslagen in Landscape.

Dat als ik hem print deze als landscape wordt uitgeprint.

Code:
 Dim fs As New IO.StreamWriter(savefiledialog1.FileName)
            fs.WriteLine("<?xml version=""1.0""?>")
            fs.WriteLine("<?mso-application progid=""Excel.Sheet""?>")
            fs.WriteLine("<ss:Workbook xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"">")
            fs.WriteLine("    <ss:Styles>")
            fs.WriteLine("        <ss:Style ss:ID=""1"">")
            fs.WriteLine("           <ss:Font ss:Bold=""1""/>")
            fs.WriteLine("        </ss:Style>")
            fs.WriteLine("    </ss:Styles>")
            fs.WriteLine("    <ss:Worksheet ss:Name=""Sheet1"">")
            fs.WriteLine("        <ss:Table>")
            For x As Integer = 0 To DataGridView1.Columns.Count - 1
                fs.WriteLine("            <ss:Column ss:Width=""{0}""/>",
                             DataGridView1.Columns.Item(x).Width)
            Next
            fs.WriteLine("            <ss:Row ss:StyleID=""1"">")
            For i As Integer = 0 To DataGridView1.Columns.Count - 1
                fs.WriteLine("                <ss:Cell>")
                fs.WriteLine(String.Format(
                             "                   <ss:Data ss:Type=""String"">{0}</ss:Data>",
                                           DataGridView1.Columns.Item(i).HeaderText))
                fs.WriteLine("                </ss:Cell>")
            Next
            fs.WriteLine("            </ss:Row>")
            For intRow As Integer = 0 To DataGridView1.RowCount - 2
                fs.WriteLine(String.Format("            <ss:Row ss:Height =""{0}"">",
                                           DataGridView1.Rows(intRow).Height))
                For intCol As Integer = 0 To DataGridView1.Columns.Count - 1
                    fs.WriteLine("                <ss:Cell>")
                    fs.WriteLine(String.Format(
                                 "                   <ss:Data ss:Type=""String"">{0}</ss:Data>",
                                               DataGridView1.Item(intCol, intRow).Value.ToString))
                    fs.WriteLine("                </ss:Cell>")
                Next
                fs.WriteLine("            </ss:Row>")
            Next
            fs.WriteLine("        </ss:Table>")
            fs.WriteLine("    </ss:Worksheet>")
            fs.WriteLine("</ss:Workbook>")
            fs.Close()
 
Sinds wanneer kun je in .XML bestanden aangeven dat het (bij voorkeur) in landscape wordt afgedrukt? Volgens mij bestaat zo'n optie niet, het is per slot geen Microsoft Word document (waar dergelijke zaken wél in aangegeven kunnen worden).

Eventueel kopeer je ergens een Word document vandaan in de nieuwste indeling (dus met .docx uitgang), hernoem die naar .zip uitgang en pak die uit.
Wellicht geeft die een extra bestandje weer bij het (basis-)XML bestand dat dergelijke instellingen bevat.

Tijs.
 
Laatst bewerkt:
Oke , dat is jammer.

Het wordt heel mooi weergegeven in het document.


Ik ga zoeken naar een alternatief, bedankt.
 
Heb wat anders gevonden , dit voldoet ook wel.

Het schrijft de inhoud van een datagridview weg naar een Excel bestand.

Is het nu wel mogelijk er ergens een regel tussen te zetten dat hij het bestand opslaat als landscape ?


Code:
  'verfying the datagridview having data or not
        If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
            Exit Sub
        End If

        'Creating dataset to export
        Dim dset As New DataSet
        'add table to dataset
        dset.Tables.Add()
        'add column to that table
        For i As Integer = 0 To DataGridView1.ColumnCount - 1
            dset.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
        Next
        'add rows to the table
        Dim dr1 As DataRow
        For i As Integer = 0 To DataGridView1.RowCount - 1
            dr1 = dset.Tables(0).NewRow
            For j As Integer = 0 To DataGridView1.Columns.Count - 1
                dr1(j) = DataGridView1.Rows(i).Cells(j).Value
            Next
            dset.Tables(0).Rows.Add(dr1)
        Next

        Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
        Dim wBook As Microsoft.Office.Interop.Excel.Workbook
        Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

        wBook = excel.Workbooks.Add()
        wSheet = wBook.ActiveSheet()

        Dim dt As System.Data.DataTable = dset.Tables(0)
        Dim dc As System.Data.DataColumn
        Dim dr As System.Data.DataRow
        Dim colIndex As Integer = 0
        Dim rowIndex As Integer = 0

        For Each dc In dt.Columns
            colIndex = colIndex + 1
            excel.Cells(1, colIndex) = dc.ColumnName
        Next

        For Each dr In dt.Rows
            rowIndex = rowIndex + 1
            colIndex = 0
            For Each dc In dt.Columns
                colIndex = colIndex + 1
                excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)

            Next
        Next

        wSheet.Columns.AutoFit()
        Dim strFileName As String = "C:\Book1.xls"
        Dim blnFileOpen As Boolean = False
        Try
            Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
            fileTemp.Close()
        Catch ex As Exception
            blnFileOpen = False
        End Try

        If System.IO.File.Exists(strFileName) Then
            System.IO.File.Delete(strFileName)
        End If

        wBook.SaveAs("C:\Book1.xls", True)
        excel.Workbooks.Open(strFileName)
        excel.Visible = True
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan