Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set the adaptive row height and column width of Excel data in C #

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to set Excel data adaptive row height and column width in C#". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In the Excel table, due to the complexity of all kinds of data, there may be problems that the column width is too wide, too narrow or the row height is too large or too small due to the different data font size and data content length in the cell. A common solution is to adjust row height and column width. In Microsoft Excel, adaptive row height or column width can be set manually in cell formatting, but through the C # code, we can use the method AutoFitColumns () or AutoFitRows () to set the adaptive row height and column width of the specified data range or the entire worksheet. Here, the adaptation is divided into the following two situations:

1. Fixed data, set row height and column width adaptive data

two。 Fixed row height and column width, set data adaptive row height and column width (that is, reduce the font size of cell data to fit cells)

Using tools: Free Spire.XLS for .NET (free version)

Note: after downloading and installing, add the reference Spire.Xls.dll file, as shown below:

[example 1] fixed data, set row height and column width adaptive data

Using Spire.Xls; namespace AutoFit_XLS {class Program {static void Main (string [] args) {/ / create an object of class Workbook and load the test document Workbook workbook = new Workbook (); workbook.LoadFromFile ("sample.xlsx"); / / get worksheet Worksheet worksheet = workbook.Worksheets [0] / / set column width and row height to adaptive (apply to the specified data range) worksheet.AllocatedRange ["A1:F15"] .AutoFitColumns (); worksheet.AllocatedRange ["A1:F15"] .AutoFitRows (); / set column width and row height to adaptive (apply to the entire worksheet) / / worksheet.AllocatedRange.AutoFitColumns () / / worksheet.AllocatedRange.AutoFitRows (); / / Save document workbook.SaveToFile ("result.xlsx", FileFormat.Version2010); System.Diagnostics.Process.Start ("result.xlsx");}

Set the effect:

[example 2] fixed row height and column width, set data adaptive row height and column width

Using Spire.Xls;namespace ShrinkTextToFitCell_XLS {class Program {static void Main (string [] args) {/ / instantiate the object of the Workbook class and load the test document Workbook workbook = new Workbook (); workbook.LoadFromFile ("test.xlsx"); / / get the first worksheet Worksheet sheet = workbook.Worksheets [0] / / get the specified data range CellRange cell = sheet.Range ["A1:C1"]; / / set the cell style (data shrinks to fit the cell) CellStyle style = cell.Style; style.ShrinkToFit = true; / / Save the document workbook.SaveToFile ("result.xlsx", ExcelVersion.Version2013) System.Diagnostics.Process.Start ("result.xlsx");}

Setting result:

This is the end of the content of "how to set Excel data adaptive row height and column width" in C#. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report