Visa och Dölj Rader, Kolumner och Skrollfält med C++
Visa och göm rader och kolumner
Aspose.Cells tillhandahåller en klass, Workbook, som representerar en Microsoft Excel-fil. Workbook klassen innehåller en Worksheets samling som tillåter utvecklare att komma åt varje arbetsblad i Excel-filen. Ett arbetsblad representeras av Worksheet-klassen. Worksheet-klassen tillhandahåller en Cells samling som representerar alla celler i arbetsbladet. Cells-samlingen innehåller flera metoder för att hantera rader eller kolumner i ett arbetsblad. Några av dessa diskuteras nedan.
Visa Rader och Kolumner
Utvecklare kan visa vilken dold rad eller kolumn som helst genom att anropa metoderna UnhideRow och UnhideColumn av Cells-samlingen respektive. Båda metoder tar två parametrar:
- Rad- eller kolumnindex - index för en rad eller kolumn som används för att visa den specifika raden eller kolumnen.
- Radhöjd eller kolumnbredd - radhöjden eller kolumnbredden tilldelad till raden eller kolumnen efter att ha visats.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Path of output excel file
U16String outputFilePath = outDir + u"output.xls";
// Create workbook
Workbook workbook(inputFilePath);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Unhiding the 3rd row and setting its height to 13.5
worksheet.GetCells().UnhideRow(2, 13.5);
// Unhiding the 2nd column and setting its width to 8.5
worksheet.GetCells().UnhideColumn(1, 8.5);
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Excel file modified and saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
worksheet->GetCells()->UnhideColumn(5, -1)
.
Dölj Rader och Kolumner
Utvecklare kan dölja en rad eller kolumn genom att anropa metoderna HideRow och HideColumn av Cells-samlingen respektive. Båda metoder tar rad- och kolumnindex som parameter för att dölja den specifika raden eller kolumnen.
#include <iostream>
#include <memory>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Path of output excel file
U16String outputFilePath = outDir + u"output.out.xls";
// Create workbook
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Hide the 3rd row of the worksheet
worksheet.GetCells().HideRow(2);
// Hide the 2nd column of the worksheet
worksheet.GetCells().HideColumn(1);
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Rows and columns hidden successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Dölj Flera Rader och Kolumner
Utvecklare kan dölja flera rader eller kolumner samtidigt genom att anropa metoderna HideRows och HideColumns av Cells-samlingen respektive. Båda metoder tar startrad- eller startkolumnindex och antalet rader eller kolumner som ska döljas som parametrar.
#include <iostream>
#include <memory>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Create workbook
Workbook workbook(inputFilePath);
// Access the first worksheet in the Excel file
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Hide 3, 4, and 5 rows in the worksheet
worksheet.GetCells().HideRows(2, 3);
// Hide 2 and 3 columns in the worksheet
worksheet.GetCells().HideColumns(1, 2);
// Save the modified Excel file
workbook.Save(outDir + u"outputxls");
std::cout << "Rows and columns hidden successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
Visa och dölj bildrullningsfält
Rullningslistor används för att navigera bland innehållet i en fil. Vanligtvis finns det två typer av rullningslistor:
- Vertikala bildrullningsfält
- Horisontella bildrullningsfält
Microsoft Excel tillhandahåller också horisontella och vertikala bildrullningsfält så att användare kan bläddra genom arbetsbladets innehåll. Med Aspose.Cells kan utvecklare kontrollera synligheten av båda typer av bildrullningsfält i Excelfiler.
Kontrollera Synligheten för Rullningslistor
Aspose.Cells tillhandahåller en klass, Workbook, som representerar en Excel-fil. Workbook-klassen erbjuder ett brett utbud av egenskaper och metoder för att hantera en Excel-fil. För att kontrollera synligheten för skrollfält, använd Workbook-klassens WorkbookSettings.IsVScrollBarVisible och WorkbookSettings.IsHScrollBarVisible egenskaper. WorkbookSettings.IsVScrollBarVisible och WorkbookSettings.IsHScrollBarVisible är Boolean-egenskaper, vilket innebär att dessa egenskaper endast kan lagra true eller false värden.
Gör bildrullningsfält synliga
Gör rullningsfält synliga genom att ställa in Workbook -klassens egenskaper WorkbookSettings.IsVScrollBarVisible eller WorkbookSettings.IsHScrollBarVisible till true.
Dölja bildrullningsfält
Dölj rullningsfält genom att ställa in egenskaperna Workbook -klassens WorkbookSettings.IsVScrollBarVisible eller WorkbookSettings.IsHScrollBarVisible till false.
Exempelkod
Nedan finns ett komplett kodexempel som öppnar en Excel-fil, book1.xls
, döljer båda skrollfälten och sedan sparar den modifierade filen som output.xls
.
#include <iostream>
#include <fstream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of input Excel file
U16String inputFilePath = srcDir + u"book1.xls";
// Path of output Excel file
U16String outputFilePath = outDir + u"output.xls";
// Create workbook from the input file
Workbook workbook(inputFilePath);
// Hide the vertical scroll bar of the Excel file
workbook.GetSettings().SetIsVScrollBarVisible(false);
// Hide the horizontal scroll bar of the Excel file
workbook.GetSettings().SetIsHScrollBarVisible(false);
// Save the modified Excel file
workbook.Save(outputFilePath);
std::cout << "Scroll bars hidden successfully and file saved!" << std::endl;
Aspose::Cells::Cleanup();
}