Formatera rader och kolumner

Arbeta med rader

Justering av radhöjd

Aspose.Cells tillhandahåller en klass,Arbetsbok , som representerar en Microsoft Excel-fil. DeArbetsbok klass innehåller enArbetsbladssamlingsom ger åtkomst till varje kalkylblad i Excel-filen. Ett arbetsblad representeras avArbetsblad klass. DeArbetsblad klass ger enCellssamling som representerar alla celler i kalkylbladet.

DeCellssamling innehåller flera metoder för att hantera rader eller kolumner i ett kalkylblad. Några av dessa diskuteras mer i detalj nedan.

Ställa in höjden på en rad

Det är möjligt att ställa in höjden på en enstaka rad genom att anropaCells samlingensSetRowHeight metod. DeSetRowHeightmetoden tar följande parametrar enligt följande:

  • Radindex, indexet för raden som du ändrar höjden på.
  • Radhöjd, radhöjden som ska tillämpas på raden.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Setting the height of the second row to 13
worksheet.Cells.SetRowHeight(1, 13);
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Ställa in höjden på alla rader i ett kalkylblad

Om utvecklare behöver ställa in samma radhöjd för alla rader i kalkylbladet kan de göra det genom att användaStandardhöjd egendom avCellssamling.

Exempel:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Setting the height of all rows in the worksheet to 15
worksheet.Cells.StandardHeight = 15;
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Arbeta med kolumner

Ställa in bredden på en kolumn

Ställ in bredden på en kolumn genom att anropaCells samlingensSetColumnWidth metod. DeSetColumnWidthmetoden tar följande parametrar:

  • Kolumnindex, indexet för kolumnen som du ändrar bredden på.
  • Kolumnbredd, önskad kolumnbredd.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Setting the width of the second column to 17.5
worksheet.Cells.SetColumnWidth(1, 17.5);
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Ställa in kolumnbredd i pixlar

Ställ in bredden på en kolumn genom att anropaCellssamlingensSetColumnWidthPixelmetod. DeSetColumnWidthPixelmetoden tar följande parametrar:

  • Kolumnindex, indexet för kolumnen som du ändrar bredden på.
  • Kolumnbreddönskad kolumnbredd i pixlar.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
string outDir = RunExamples.Get_OutputDirectory();
//Load source Excel file
Workbook workbook = new Workbook(sourceDir + "Book1.xlsx");
//Access first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Set the width of the column in pixels
worksheet.Cells.SetColumnWidthPixel(7, 200);
workbook.Save(outDir + "SetColumnWidthInPixels_Out.xlsx");

Ställa in bredden på alla kolumner i ett kalkylblad

För att ställa in samma kolumnbredd för alla kolumner i kalkylbladet, användCells samlingensStandardbreddfast egendom.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Setting the width of all columns in the worksheet to 20.5
worksheet.Cells.StandardWidth = 20.5;
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Förhandsämnen