Izvoz v XLSX: Razlika med redakcijama

Iz SDMS
Jump to navigationJump to search
m
m (Prenos podatkov iz baze)
Vrstica 1: Vrstica 1:
 +
== Osnovni primer ==
 +
<pre>
 +
Sub PrimerXLS
 +
  Dim XLS As TXlsWriter
 +
  Dim titleStyle, headerStyle, cellStyle, footerStyle As TXlsStyle
 +
  Dim lefttopCell, rightbottomCell As TXlsCell
 +
  Dim J As Long
 +
 +
  // inicializiramo writer in  prvi list
 +
  XLS = CreateXlsWriter
 +
  XLS.AddSheet('List 1')
 +
 +
  // spremenimo normal stil
 +
  XLS.StyleNormal.FontSize = 12
 +
  XLS.StyleNormal.FontName = 'Arial'
 +
 +
  // definiramo ostale stile po potrebi
 +
  titleStyle = XLS.AddStyle('', XLS.StyleTitle)
 +
  titleStyle.FontBold = False
 +
  titleStyle.FontItalic = True
 +
  titleStyle.FontSize = 20
 +
  titleStyle.HorAlign = XlshaCenter
 +
  titleStyle.FontName = 'Arial'
 +
 +
  headerStyle = XLS.AddStyle('', XLS.StyleHeader)
 +
  headerStyle.FontSize = 12
 +
  headerStyle.HorAlign = xlshaAuto
 +
 +
  footerStyle = XLS.AddStyle('', XLS.StyleFooter)
 +
  footerStyle.FontSize = 12
 +
  footerStyle.HorAlign = XlshaRight
 +
  footerStyle.TextWrap = false
 +
 +
  // zamrznemo stolpce in/ali vrstice
 +
  XLS.SetFixedColumns(1)
 +
  XLS.SetFixedRows(2)
 +
 +
  // dodamo naslov
 +
  XLS.AddCell('Naslov', xlsnfText, titleStyle)
 +
  XLS.MergeCells('A1', 'E1') // združimo celice za naslov
 +
 +
  // dodamo header vrstico
 +
  XLS.AddRow
 +
  XLS.AddCell('', xlsnfText, headerStyle)
 +
  lefttopCell = XLS.GetCurCell // nastavimo referenco na zgoraj levo celico tabele
 +
  XLS.AddCell('Stolpec A', xlsnfText, headerStyle)
 +
  XLS.AddCell('Stolpec B', xlsnfText, headerStyle)
 +
  XLS.AddCell('Stolpec C', xlsnfText, headerStyle)
 +
  XLS.AddCell('Seštevek', xlsnfText, headerStyle)
 +
 +
  // dodamo podatke v tabelo
 +
  for J = 1 to 5
 +
    XLS.AddRow
 +
    XLS.AddCell('Vrstica ' + IntToStr(J), xlsnfText, headerStyle)
 +
    XLS.AddCell(IntToStr(Random(1000)), xlsnfLong, nil)
 +
    XLS.AddCell(IntToStr(Random(1000)), xlsnfLong, nil)
 +
    XLS.AddCell(IntToStr(Random(1000)), xlsnfLong, nil)
 +
    XLS.AddCell(xlscvAutoRowSum, xlsnfLong, footerStyle) // dodamo seštevek numeričnih celic v levo
 +
  next
 +
 +
  // dodamo footer vrstico
 +
  XLS.AddRow
 +
  XLS.AddCell('Seštevek', xlsnfText, headerStyle)
 +
  XLS.AddCell(xlscvAutoColSum, xlsnfLong, footerStyle) // dodamo seštevek numeričnih celic navzgor
 +
  XLS.AddCell(xlscvAutoColSum, xlsnfLong, footerStyle)
 +
  XLS.AddCell(xlscvAutoColSum, xlsnfLong, footerStyle)
 +
  XLS.AddCell(xlscvAutoColSum, xlsnfLong, footerStyle)
 +
  rightbottomCell = XLS.GetCurCell // nastavimo referenco na spodnjo desno celico tabele
 +
 +
  // narišemo robove okoli headerja, footerja in okoli celotne tabele
 +
  XLS.SetBorders(lefttopCell.Address, lefttopCell.RelativeAddress(4,0), xlsbtAll, xlsbsThick, xlsbsUnchanged, Black) // header
 +
  XLS.SetBorders(rightbottomCell.RelativeAddress(-4,0), rightbottomCell.Address, xlsbtAll, xlsbsThick, xlsbsUnchanged, Black) // footer
 +
  XLS.SetBorders(lefttopCell.Address, rightbottomCell.Address, xlsbtAll, xlsbsThick, xlsbsUnchanged, Black) // cela tabela
 +
EndSub
 +
</pre>
 +
 
== Prenos podatkov iz baze ==
 
== Prenos podatkov iz baze ==
 
<pre>
 
<pre>
Vrstica 55: Vrstica 131:
  
 
Sub RoboviXLS(XLS As TXlsWriter)
 
Sub RoboviXLS(XLS As TXlsWriter)
   XLS.SetBorders(XLS.Table.LeftTop.RelativeAddress(-1,0), XLS.Table.RightTop.RelativeAddress(-1,0), xlsbtAll, xlsbsMedium, xlsbsUnchanged, Black) // obroba naslova
+
   XLS.SetBorders(XLS.Table.LeftTop.RelativeAddress(0, -1), XLS.Table.RightTop.RelativeAddress(0, -1), xlsbtAll, xlsbsMedium, xlsbsUnchanged, Black) // obroba naslova
 
   XLS.SetBorders(XLS.Table.LeftTop.Address, XLS.Table.RightBottom.Address, xlsbtHor, xlsbsUnchanged, xlsbsThin, Black) // horizontalne črte jedra tabele
 
   XLS.SetBorders(XLS.Table.LeftTop.Address, XLS.Table.RightBottom.Address, xlsbtHor, xlsbsUnchanged, xlsbsThin, Black) // horizontalne črte jedra tabele
 
   XLS.SetBorders(XLS.Table.LeftTop.Address, XLS.Table.RightTop.Address, xlsbtAll, xlsbsMedium, xlsbsUnchanged, Black) // obroba headerja
 
   XLS.SetBorders(XLS.Table.LeftTop.Address, XLS.Table.RightTop.Address, xlsbtAll, xlsbsMedium, xlsbsUnchanged, Black) // obroba headerja

Redakcija: 16:50, 11. marec 2021

Osnovni primer

Sub PrimerXLS
  Dim XLS As TXlsWriter
  Dim titleStyle, headerStyle, cellStyle, footerStyle As TXlsStyle
  Dim lefttopCell, rightbottomCell As TXlsCell
  Dim J As Long

  // inicializiramo writer in  prvi list
  XLS = CreateXlsWriter
  XLS.AddSheet('List 1')

  // spremenimo normal stil
  XLS.StyleNormal.FontSize = 12
  XLS.StyleNormal.FontName = 'Arial'

  // definiramo ostale stile po potrebi
  titleStyle = XLS.AddStyle('', XLS.StyleTitle)
  titleStyle.FontBold = False
  titleStyle.FontItalic = True
  titleStyle.FontSize = 20
  titleStyle.HorAlign = XlshaCenter
  titleStyle.FontName = 'Arial'

  headerStyle = XLS.AddStyle('', XLS.StyleHeader)
  headerStyle.FontSize = 12
  headerStyle.HorAlign = xlshaAuto

  footerStyle = XLS.AddStyle('', XLS.StyleFooter)
  footerStyle.FontSize = 12
  footerStyle.HorAlign = XlshaRight
  footerStyle.TextWrap = false

  // zamrznemo stolpce in/ali vrstice
  XLS.SetFixedColumns(1)
  XLS.SetFixedRows(2)

  // dodamo naslov
  XLS.AddCell('Naslov', xlsnfText, titleStyle)
  XLS.MergeCells('A1', 'E1') // združimo celice za naslov

  // dodamo header vrstico
  XLS.AddRow
  XLS.AddCell('', xlsnfText, headerStyle)
  lefttopCell = XLS.GetCurCell // nastavimo referenco na zgoraj levo celico tabele
  XLS.AddCell('Stolpec A', xlsnfText, headerStyle)
  XLS.AddCell('Stolpec B', xlsnfText, headerStyle)
  XLS.AddCell('Stolpec C', xlsnfText, headerStyle)
  XLS.AddCell('Seštevek', xlsnfText, headerStyle)

  // dodamo podatke v tabelo
  for J = 1 to 5
    XLS.AddRow
    XLS.AddCell('Vrstica ' + IntToStr(J), xlsnfText, headerStyle)
    XLS.AddCell(IntToStr(Random(1000)), xlsnfLong, nil)
    XLS.AddCell(IntToStr(Random(1000)), xlsnfLong, nil)
    XLS.AddCell(IntToStr(Random(1000)), xlsnfLong, nil)
    XLS.AddCell(xlscvAutoRowSum, xlsnfLong, footerStyle) // dodamo seštevek numeričnih celic v levo
  next

  // dodamo footer vrstico
  XLS.AddRow
  XLS.AddCell('Seštevek', xlsnfText, headerStyle)
  XLS.AddCell(xlscvAutoColSum, xlsnfLong, footerStyle) // dodamo seštevek numeričnih celic navzgor
  XLS.AddCell(xlscvAutoColSum, xlsnfLong, footerStyle)
  XLS.AddCell(xlscvAutoColSum, xlsnfLong, footerStyle)
  XLS.AddCell(xlscvAutoColSum, xlsnfLong, footerStyle)
  rightbottomCell = XLS.GetCurCell // nastavimo referenco na spodnjo desno celico tabele

  // narišemo robove okoli headerja, footerja in okoli celotne tabele
  XLS.SetBorders(lefttopCell.Address, lefttopCell.RelativeAddress(4,0), xlsbtAll, xlsbsThick, xlsbsUnchanged, Black) // header
  XLS.SetBorders(rightbottomCell.RelativeAddress(-4,0), rightbottomCell.Address, xlsbtAll, xlsbsThick, xlsbsUnchanged, Black) // footer
  XLS.SetBorders(lefttopCell.Address, rightbottomCell.Address, xlsbtAll, xlsbsThick, xlsbsUnchanged, Black) // cela tabela
EndSub

Prenos podatkov iz baze

Sub TabelaXLS
  Dim XLS As TXlsWriter, XLSF As TXlsField
  Dim L as TLayer

  // inicializiramo writer in  prvi list
  XLS = CreateXlsWriter
  XLS.AddSheet('List 1')

  // pripravimo definicijo stolpcev
  XLS.Table.Clear                                          // prvi klic je opcijski, počisti definicijo stolpcev
  XLSF = XLS.Table.AddCounter('Zap. št.')                  // zapisi bodo oštevilceni
  XLSF.FooterValue = 'Skupaj'                              // v footerju bo fiksen tekst
  XLS.Table.AddFieldByName('Naslov polja', 'polje1')       // fiksno nastavimo ime stolpca
  XLS.Table.AddFieldByName('', 'polje2')                   // ime stolpca je ime polja
  XLSF = XLS.Table.AddFieldByName('Število', 'polje3')     // numerično polje
  XLSF.FooterValue = xlscvAutoColSum                       // v footerju bo vsota stolpca
  XLSF = XLS.Table.AddFormula('Vsota 3 in 4', 'IntToStr([polje3].AsNumber + [polje4].AsNumber)') // Basic formula
  XLSF.FooterValue = xlscvAutoColSum                       // v footerju bo vsota stolpca
  XLSF = XLS.Table.AddValue('Vsota levo', xlscvAutoRowSum) // v stolpcu bo vsota številk v vrstici
  XLSF.FooterValue = xlscvAutoColSum                       // v footerju bo vsota stolpca

  // pripravimo podatke
  L = [Layer]
  L.Filter.ExecuteSQL('[polje1] = 10')
  L.Sort.Load('Default')
  L.Sort.Execute

  // zapišemo podatke po dani definiciji
  XLS.Table.AddRecordList('Naslov', L.FilteredRecords) // naslov je opcijski

  // nastavimo še robove celic
  RoboviXLS(XLS)

  // dodamo nov list
  XLS.AddSheet('List 2')

  // pripravimo nove podatke
  L = [Layer]
  L.Filter.ExecuteSQL('[polje1] = 20')
  L.Sort.Load('Default')
  L.Sort.Execute
  
  // zapišemo podatke po dani definiciji
  XLS.Table.AddRecordList('Naslov', L.FilteredRecords) // naslov je opcijski

  // nastavimo še robove celic
  RoboviXLS(XLS)

  // shranimo v file in sprostimo writer
  XLS.SaveToFile('c:\temp\test.xlsx')
  XLS.Destroy
EndSub

Sub RoboviXLS(XLS As TXlsWriter)
  XLS.SetBorders(XLS.Table.LeftTop.RelativeAddress(0, -1), XLS.Table.RightTop.RelativeAddress(0, -1), xlsbtAll, xlsbsMedium, xlsbsUnchanged, Black) // obroba naslova
  XLS.SetBorders(XLS.Table.LeftTop.Address, XLS.Table.RightBottom.Address, xlsbtHor, xlsbsUnchanged, xlsbsThin, Black) // horizontalne črte jedra tabele
  XLS.SetBorders(XLS.Table.LeftTop.Address, XLS.Table.RightTop.Address, xlsbtAll, xlsbsMedium, xlsbsUnchanged, Black) // obroba headerja
  XLS.SetBorders(XLS.Table.LeftTop.Address, XLS.Table.RightBottom.Address, xlsbtAll, xlsbsMedium, xlsbsUnchanged, Black) // obroba tabele
  XLS.SetBorders(XLS.Table.LeftBottom.Address, XLS.Table.RightBottom.Address, xlsbtAll, xlsbsMedium, xlsbsUnchanged, Black) // obroba footerja
EndSub