Leere Zeilen und Spalten beim Export von Tabellen in CSV Format mit JavaScript via C++ trimmen
Mögliche Verwendungsszenarien
Manchmal enthält Ihre Excel- oder CSV-Datei führende leere Spalten oder Zeilen. Beispielweise betrachten Sie diese Zeile
,,,data1,data2
Hier sind die ersten drei Zellen oder Spalten leer. Wenn Sie eine solche CSV-Datei in Microsoft Excel öffnen, verwirft Microsoft Excel diese führenden leeren Zeilen und Spalten.
Standardmäßig löst Aspose.Cells for JavaScript via C++ beim Speichern keine führenden leeren Spalten und Zeilen aus, aber wenn Sie diese entfernen möchten, wie es Microsoft Excel tut, dann bietet Aspose.Cells die TxtSaveOptions.trimLeadingBlankRowAndColumn-Eigenschaft. Bitte setzen Sie sie auf true und dann werden alle führenden leeren Zeilen und Spalten beim Speichern verworfen.
Führende leere Zeilen und Spalten beim Export von Tabellenkalkulationen in das CSV-Format abschneiden
Der folgende Beispielcode lädt die Quellexcel-Datei, die zwei führende leere Spalten enthält. Er speichert die Excel-Datei zuerst im CSV-Format ohne Änderungen und setzt dann die TxtSaveOptions.trimLeadingBlankRowAndColumn-Eigenschaft auf true, um sie erneut zu speichern. Der Screenshot zeigt die Quellexcel-Datei, die CSV-Ausgabedatei ohne Kürzung und die CSV-Ausgabedatei mit Kürzung.

Beispielcode
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Trim Blank Columns</title>
</head>
<body>
<h1>Trim Blank Columns Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink1" style="display: none;">Download Result 1</a>
<a id="downloadLink2" style="display: none; margin-left: 10px;">Download Result 2</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, TxtSaveOptions, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Load source workbook
const wb = new Workbook(new Uint8Array(arrayBuffer));
// Save in csv format (without trimming)
const outputData1 = wb.save(SaveFormat.Csv);
const blob1 = new Blob([outputData1]);
const downloadLink1 = document.getElementById('downloadLink1');
downloadLink1.href = URL.createObjectURL(blob1);
downloadLink1.download = 'outputWithoutTrimBlankColumns.csv';
downloadLink1.style.display = 'inline-block';
downloadLink1.textContent = 'Download CSV Without Trimming';
// Now save again with TrimLeadingBlankRowAndColumn as true
const opts = new TxtSaveOptions();
opts.trimLeadingBlankRowAndColumn = true;
// Save in csv format (with trimming)
const outputData2 = wb.save(opts);
const blob2 = new Blob([outputData2]);
const downloadLink2 = document.getElementById('downloadLink2');
downloadLink2.href = URL.createObjectURL(blob2);
downloadLink2.download = 'outputTrimBlankColumns.csv';
downloadLink2.style.display = 'inline-block';
downloadLink2.textContent = 'Download CSV With Trimmed Blank Columns';
resultDiv.innerHTML = '<p style="color: green;">Files generated. Use the links above to download the CSVs.</p>';
});
</script>
</html>