c# asp net 设置 excel 列宽
2012-07-28 17:13
295 查看
Here's a quick tip to get started working with Excel interop in C#.
// Create a new instance of the Excel application
excelFile = new Excel.ApplicationClass();
Excel.Workbook workbook = excelFile.Workbooks.Add(Type.Missing); // Now create a brand new workbook
excelFile.Visible = true; // ensure that the excel app is visible.
Worksheet ws = (Worksheet)excelFile.ActiveSheet; // Get the current active worksheet.
Microsoft.Office.Interop.Excel.Worksheet worksheet2 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(2); //Get more work sheet if neccessary
ws.Activate();
//Here is how to set the column Width
public void SetColumnWidth(Worksheet ws, int col, int width)
{
((Range)ws.Cells[1, col]).EntireColumn.ColumnWidth = width;
}
// Apply the setting so that it would autofit to contents
public void AutoFitColumn(Worksheet ws, int col)
{
((Range)ws.Cells[1, col]).EntireColumn.AutoFit();
}
How to Set Row to Bold in Excel C# Interop:
((Range)ws.Cells[row, 1]).EntireRow.Font.Bold = true;
How to Set a value inside a cell in Excel:
(Range)ws.Cells[row, column]).Value2 = item; // set the cell value at a row and column
How to Format the Entire Column:
((Range)ws.Cells[1, col]).EntireColumn.NumberFormat = format;
// Create a new instance of the Excel application
excelFile = new Excel.ApplicationClass();
Excel.Workbook workbook = excelFile.Workbooks.Add(Type.Missing); // Now create a brand new workbook
excelFile.Visible = true; // ensure that the excel app is visible.
Worksheet ws = (Worksheet)excelFile.ActiveSheet; // Get the current active worksheet.
Microsoft.Office.Interop.Excel.Worksheet worksheet2 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(2); //Get more work sheet if neccessary
ws.Activate();
//Here is how to set the column Width
public void SetColumnWidth(Worksheet ws, int col, int width)
{
((Range)ws.Cells[1, col]).EntireColumn.ColumnWidth = width;
}
// Apply the setting so that it would autofit to contents
public void AutoFitColumn(Worksheet ws, int col)
{
((Range)ws.Cells[1, col]).EntireColumn.AutoFit();
}
How to Set Row to Bold in Excel C# Interop:
((Range)ws.Cells[row, 1]).EntireRow.Font.Bold = true;
How to Set a value inside a cell in Excel:
(Range)ws.Cells[row, column]).Value2 = item; // set the cell value at a row and column
How to Format the Entire Column:
((Range)ws.Cells[1, col]).EntireColumn.NumberFormat = format;
相关文章推荐
- C# 对Excel 单元格格式, 及行高、 列宽、 单元格边框线、 冻结设置
- C#中操作刚导出的Excel,设置其为自动调整列宽
- c#中设置EXCEL的行高或者是列宽
- C# Excel 设置行高,列宽,合并单元格,单元格边框线,冻结 c#操作excel大全
- C#:一些关于Excel的操作(设置单元格格式、合并单元格)
- 使用C#和Excel进行报表开发(七)-设置单元格的显示格式
- Jxl导出Excel(跨行、设置列宽、设置行高、自动对齐、自定义背景等)
- C# 设置Excel 首行冻结窗口
- C#导出Excel按照指定格式设置单元格属性值
- 使用C#和Excel进行报表开发(七)-设置单元格的显示格式
- C#导出带有格式的Excel(列宽,合并单元格,显示边框线,加背景颜色等)
- POI中设置Excel单元格格式(背景色,居中,字体,边框,列宽,行高,自动换行等)
- C# Excel 行高,列宽,合并单元格,单元格边框线,冻结
- java用POI设置Excel的列宽
- C#设置Excel单元格格式
- C#中设置Excel单元格格式
- C# 设置 Excel 条件格式 与 冻结窗口
- C# 导出Excel的单元格属性设置
- C# EXCEL, EXCEL的各种设置
- C# Excel 行高,列宽,合并单元格,单元格边框线,冻结