Leabharlann Swift Saor le Léamh agus Scríobh Comhaid Excel XLSX
Leabharlann Swift 5 láidir foinse oscailte a chuireann cumhacht ar fhorbróirí chun comhaid XLSX a chruthú agus a léamh, cóipeáil, bileoga a bhaint, stíleanna agus formáidiú a chur i bhfeidhm, srl. saor in aisce.
Cad is XlsxReaderWriterSwift ann?
Tuirseach ó thrí chéile le formáidí casta de bhileoga oibre i d'fhorbhróirí Swift? Samhlaigh cás ina gá d'fheidhmchlár tuairisc mhionsonraithe a ghiniúint, sonraí úsáideoirí a allmhairiú ó teimpléad, nó fiú samhail airgeadais dinimiciúil a thógáil — go léir laistigh den chórais iOS. Tá parsáil láimhe de chomhaid CSV teoranta, agus tá brath ar sheirbhísí seachtracha chun comhaid XLSX a láimhseáil anaithnid. Seo áit a thagann XlsxReaderWriterSwift, leabharlann cumhachtach agus dúchasach open‑source, chun cabhrú. Soláthraíonn sé toolkit intuítíve agus láidir do fhorbróirí Swift chun an formáid .xlsx nua‑aimseartha de chuid Microsoft Excel a léamh agus a scríobh go réidh, go díreach i d’aipí iOS, macOS, agus visionOS.
Is leabharlann Swift 5 láidir XlsxReaderWriterSwift í a chuireann cumhacht i bhfeidhm ar fhorbróirí bogearraí chun comhaid XLSX a chruthú agus a léamh, an caighdeán do bhileoga oibre Excel nua‑aimseartha. Soláthraíonn an leabharlann gnéithe tábhachtacha, mar léamh comhaid XLSX, cruthú comhaid XLSX nua, cur i bhfeidhm stíle agus formáidiú ar chill, cur íomhánna leis agus iad a bhainistiú i gcolúin, gineadh tuairiscí, leagán ar spreadsheetanna, agus go leor eile. Tógtha ar bhun le leabharlann C libxlsxwriter, tugann sé raon cuimsitheach gnéithe d'ecosystem Apple. Ciallaíonn sé seo gur féidir leat comhaid Excel lán‑ghnéitheacha, comhoiriúnacha 100 % a ghiniúint díreach ó d'iarratais iOS nó macOS.
Ag Tosú le XlsxReaderWriterSwift
Is é an bealach molta chun XlsxReaderWriterSwift a shuiteáil ná úsáid a bhaint as CocoaPods. Úsáid an t-ordú seo a leanas le suiteáil réidh le do thoil.
Installaigh XlsxReaderWriterSwift trí CocoaPods
pod "XlsxReaderWriterSwift"
pod install
Installaigh XlsxReaderWriterSwift trí GitHub
git clone https://github.com/mehulparmar4ever/XlsxReaderWriterSwift.git
Is féidir leat an comhad a íoslódáil go díreach ó GitHub.
Cruthaigh & Scríobhaigh i gComhad XLSX Nua trí Leabharlann Swift
Is chomh éasca atá sé comhad nua a chruthú ón ngnáth-phointe agus a líonadh le sonraí. Tá tacaíocht iomlán curtha ar fáil ag an leabharlann oscailte XlsxReaderWriterSwift chun cnaipí Excel XLSX nua a chruthú agus a bhainistiú laistigh de fheidhmchláir Swift. Is féidir le forbróirí bogearraí cnaipí nua a chruthú, na hinchearta reatha a athainmnigh, sonraí na gcnáipí a chóipeáil, an cháipéis a shábháil i suíomh ar leith, srl. Léiríonn an sampla thíos conas is féidir le forbróirí bogearraí cnaipí Excel XLSX nua a chruthú ag baint úsáide as an leabharlann Swift.
Conas Bileog Oibre Nua Excel XLSX a Chruthú trí Leabharlann Swift?
import XlsxReaderWriterSwift
// 1. Create a new, empty document
let document = BRAOfficeDocumentPackage()
// 2. Get the first worksheet and give it a name
guard let worksheet = document.workbook.worksheets.first as? BRAWorksheet else {
return
}
worksheet.name = "User Data"
// 3. Write different types of data to cells
worksheet.cell(forCellReference: "A1", shouldCreate: true).stringValue = "Name"
worksheet.cell(forCellReference: "B1", shouldCreate: true).stringValue = "Score"
worksheet.cell(forCellReference: "A2", shouldCreate: true).stringValue = "Alice"
worksheet.cell(forCellReference: "B2", shouldCreate: true).integerValue = 95
worksheet.cell(forCellReference: "A3", shouldCreate: true).stringValue = "Bob"
worksheet.cell(forCellReference: "B3", shouldCreate: true).integerValue = 87
// 4. Define a file path to save the document
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = documentsDirectory.appendingPathComponent("NewReport.xlsx")
// 5. Save the document to the specified path
do {
let data = document.save()
try data?.write(to: fileURL)
print("Spreadsheet saved successfully at: \(fileURL.path)")
} catch {
print("Failed to save spreadsheet: \(error)")
}
Cuir Stíle Cille Ardroghnaithe i bhfeidhm trí Swift
Ní hamháin go bhfuil na cnaipí faoi bhunús sonraí – tá ceannródaíocht ag an bhpreas. Soláthraíonn an API oscailte XlsxReaderWriterSwift rialú fairsinge ar stíleanna na gcnáip. Téann sé i dtreo níos mó ná iontráil shimplí de shonraí. Is féidir leat raon leathan roghanna formáidithe a chur i bhfeidhm, lena n-áirítear comhréir cnáipí, formáid uimhreacha, cnáipí nua a dhéanamh, stíleanna nua a chruthú, rochtain a fháil ar stíleanna atá ann cheana, imeall a chur i bhfeidhm ar raon cnáip, srl. Seo sampla simplí a thaispeánann conas is féidir le forbróirí bogearraí stíleanna forbartha a chur i bhFeidhm ar chnaipí Excel ag baint úsáide as orduithe Swift.
Conas Stíleanna Casta a chur i bhFeidhm ar Chill Excel ag úsáid Leabharlann Swift?
// ... (after creating a worksheet and cell)
// Get or create a cell
let headerCell = worksheet.cell(forCellReference: "A1", shouldCreate: true)
headerCell.stringValue = "Sales Report"
// Access the cell's style
let style = headerCell.cellStyle()
// Configure the style
style.font.bold = true
style.font.size = 18
style.fill.foregroundColor = .blue // Background color
style.fill.patternType = .solid
style.alignment.horizontal = .center
// Apply a border to a range of cells
let borderStyle = BRABorderStyle()
borderStyle.lineStyle = .medium
borderStyle.color = .darkGray
let dataRange = worksheet.cells(inRows: 1...3, columns: 1...2)
for cell in dataRange {
(cell as? BRACell)?.cellStyle().bottomBorder = borderStyle
}
Léamh Comhad XLSX trí Leabharlann Swift
Leis an leabharlann oscailte XlsxReaderWriterSwift, is féidir le forbróirí bogearraí sonraí a láimhseáil laistigh de Spreadsheet Excel tríd an API Swift. Ceadaíonn an leabharlann oibríochtaí éagsúla láimhseála sonraí, mar shampla sórtáil, scagadh, agus luachanna a ríomh taobh istigh de bhileoga Excel. Déanann sé seo uirlis iontach do thascanna ar nós anailís sonraí agus tuarascáil. Ina theannta sin, is féidir le forbróirí bogearraí sonraí áirithe a bhaint as cnáipí, rótaí nó colúin ar leith laistigh de bhileog Excel trína n-API Swift. Tá sé seo thar a bheith úsáideach nuair a bhaintear le tacair mhóra sonraí agus níl gá ach le faisnéis shonrach.
Conas Sonraí a Léamh ó Chomhad Excel XLSX le API Swift Saor?
import XlsxReaderWriterSwift
// 1. Locate the .xlsx file in your app's bundle
guard let filePath = Bundle.main.path(forResource: "SampleData", ofType: "xlsx") else {
print("File not found!")
return
}
// 2. Create a BRAOfficeDocumentPackage object from the file
let document = BRAOfficeDocumentPackage.open(filePath)
// 3. Get the first worksheet
guard let worksheet = document.workbook.worksheets.first as? BRAWorksheet else {
print("No worksheets found!")
return
}
// 4. Read data from specific cells
if let cellA1 = worksheet.cell(forCellReference: "A1") {
let stringValue = cellA1.stringValue() // Gets the value as a String
print("A1: \(stringValue)")
}
if let cellB2 = worksheet.cell(forCellReference: "B2") {
let numberValue = cellB2.floatValue() // Gets the value as a Float
print("B2: \(numberValue)")
}
// 5. Iterate over a range of cells
for row in 1...5 {
for column in 1...3 {
let cellReference = "\(UnicodeScalar(64 + column)!)\(row)" // Creates refs like A1, B1, etc.
if let cell = worksheet.cell(forCellReference: cellReference),
let value = cell.stringValue() {
print("\(cellReference): \(value)")
}
}
}