Gestisci i fogli di lavoro dei file Microsoft Excel con JavaScript tramite C++
Aspose.Cells fornisce una classe, Workbook che rappresenta un file Excel. La classe Workbook contiene una collezione worksheets che consente di accedere a ogni foglio di lavoro nel file Excel.
Un foglio di lavoro è rappresentato dalla classe Worksheet. La classe Worksheet offre un’ampia gamma di proprietà e metodi per gestire i fogli di lavoro.
Aggiungere fogli di lavoro a un nuovo file Excel
Per creare un nuovo file Excel in modo programmatico:
- Crea un oggetto della classe Workbook.
- Chiama il metodo WorksheetCollection.add(SheetType) della classe WorksheetCollection. Un foglio di lavoro vuoto viene aggiunto automaticamente al file Excel. Può essere referenziato passando l’indice del foglio di lavoro appena creato alla collezione worksheets.
- Ottenere un riferimento al foglio di lavoro.
- Eseguire operazioni sui fogli di lavoro.
- Salvare il nuovo file Excel con nuovi fogli di lavoro chiamando il metodo Workbook.save(string, SaveFormat) della classe Workbook.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells - Add Worksheet Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = 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 () => {
// Instantiate a new Workbook
const workbook = new Workbook();
// Get current worksheet count (converted from getWorksheets().getCount())
const i = workbook.worksheets.count;
// Add a new worksheet (converted from getWorksheets().add())
workbook.worksheets.add();
// Obtain the newly added worksheet by index (converted from getWorksheets().get(i))
const worksheet = workbook.worksheets.get(i);
// Set the name of the newly added worksheet (converted from setName)
worksheet.name = "My Worksheet";
// Save the workbook to XLS format and prepare download
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook created and saved. Click the download link to get the file.</p>';
});
</script>
</html>
Aggiunta di fogli di lavoro a un foglio di lavoro progettato
Il processo di aggiunta di fogli di lavoro a un foglio di lavoro di progettazione è lo stesso di aggiungere un nuovo foglio di lavoro, tranne per il fatto che il file Excel esiste già e deve essere aperto prima di aggiungere i fogli di lavoro. Un file di progettazione può essere aperto dalla classe Workbook.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Add Worksheet</title>
</head>
<body>
<h1>Add Worksheet Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Worksheet, Cell } = 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');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Adding a new worksheet to the Workbook object
const i = workbook.worksheets.add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.worksheets.get(i);
// Setting the name of the newly added worksheet
worksheet.name = "My Worksheet";
// Saving the Excel file
const outputData = workbook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Worksheet added successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Accesso ai fogli di lavoro utilizzando il nome del foglio
Accedi a qualsiasi foglio di lavoro specificando il suo nome o indice.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example: Read Cell Value</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Worksheet, Cell } = 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');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object by opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Accessing a worksheet using its sheet name
const worksheet = workbook.worksheets.get("Sheet1");
const cell = worksheet.cells.get("A1");
console.log(cell.value);
document.getElementById('result').innerHTML = `<p>Cell A1 value: ${cell.value}</p>`;
});
</script>
</html>
Rimozione dei fogli di lavoro utilizzando il nome del foglio
Per rimuovere fogli di lavoro da un file, chiama il metodo WorksheetCollection.removeAt(string) della classe WorksheetCollection. Passa il nome del foglio al metodo WorksheetCollection.removeAt(string) per rimuovere un foglio di lavoro specifico.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = 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');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Removing a worksheet using its sheet name
workbook.worksheets.removeAt("Sheet1");
// Save workbook
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Worksheet removed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Rimozione dei fogli di lavoro utilizzando l’indice del foglio
Rimuovere fogli di lavoro per nome funziona bene quando si conosce il nome del foglio di lavoro. Se non si conosce il nome, usare una versione sovraccaricata del metodo WorksheetCollection.removeAt(string) che prende l’indice del foglio di lavoro invece del suo nome.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example - Remove First Worksheet</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = 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');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object
// Opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Removing a worksheet using its sheet index
workbook.worksheets.removeAt(0);
// Saving the modified Excel file
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Worksheet removed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Attivare fogli e fare di una cella attiva nel foglio di lavoro
A volte, è necessario attivare e visualizzare un foglio di lavoro specifico quando un utente apre un file Microsoft Excel in Excel. Allo stesso modo, si potrebbe voler attivare una cella specifica e impostare le barre di scorrimento per mostrare la cella attiva. Aspose.Cells è in grado di eseguire tutte queste operazioni.
Un foglio attivo è un foglio su cui stai lavorando: il nome del foglio attivo sulla scheda è in grassetto per impostazione predefinita.
Una cella attiva è una cella selezionata, la cella in cui i dati vengono inseriti quando si inizia a digitare. Solo una cella è attiva alla volta. La cella attiva è evidenziata da un bordo spesso.
Attivare fogli e rendere una cella attiva
Aspose.Cells fornisce chiamate API specifiche per attivare un foglio e una cella. Ad esempio, la proprietà WorksheetCollection.activeSheetIndex è utile per impostare il foglio attivo in una cartella di lavoro. Allo stesso modo, la proprietà Worksheet.activeCell viene utilizzata per impostare e ottenere una cella attiva nel foglio di lavoro.
Per assicurarti che le barre di scorrimento orizzontali o verticali siano posizionate alla riga e alla colonna di indice desiderati per mostrare dati specifici, usa le proprietà Worksheet.firstVisibleRow e Worksheet.firstVisibleColumn.
L’esempio seguente mostra come attivare un foglio di lavoro e rendere una cella attiva in esso. Nell’output generato, le barre di scorrimento verranno scorrere per fare in modo che la seconda riga e la seconda colonna siano la loro prima riga e colonna visibile.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Create Workbook Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = 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 () => {
// Instantiate a new Workbook.
const workbook = new Workbook();
// Add a worksheet if collection is empty
const worksheets = workbook.worksheets;
if (worksheets.count === 0) {
worksheets.add();
}
// Get the first worksheet in the workbook.
const worksheet1 = worksheets.get(0);
// Get the cells in the worksheet.
const cells = worksheet1.cells;
// Input data into B2 cell.
const cell = cells.get(1, 1);
cell.value = "Hello World!";
// Set the first sheet as an active sheet.
worksheets.activeSheetIndex = 0;
// Set B2 cell as an active cell in the worksheet.
worksheet1.activeCell = "B2";
// Set the B column as the first visible column in the worksheet.
worksheet1.firstVisibleColumn = 1;
// Set the 2nd row as the first visible row in the worksheet.
worksheet1.firstVisibleRow = 1;
// Save the excel file.
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook created successfully! Click the download link to get the file.</p>';
});
</script>
</html>
Argomenti avanzati
- Copia e Sposta Fogli di Lavoro
- Contare il numero di celle nel foglio di lavoro
- Rilevamento di fogli di lavoro vuoti
- Trova se il foglio di lavoro è un foglio di dialogo
- Ottieni l’ID univoco del foglio di lavoro
- Creare, Manipolare o Rimuovere scenari dai fogli di lavoro
- Gestione interruzioni di pagina
- Funzionalità Impostazioni pagina
- Utilizza la proprietà Sheet.SheetId di OpenXml utilizzando Aspose.Cells
- Visualizzazioni del foglio di lavoro