Roc*_*etq 19 c# excel office-interop
我正在使用标准库
using Excel = Microsoft.Office.Interop.Excel;
Run Code Online (Sandbox Code Playgroud)
这就是我创建Excel的方式,只是代码的一小部分:
//Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
Excel._Application xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//add data
xlWorkSheet.Cells[1, 1] = "";
xlWorkSheet.Cells[1, 2] = "Student1";
xlWorkSheet.Cells[1, 3] = "Student2";
xlWorkSheet.Cells[1, 4] = "Student3";
Run Code Online (Sandbox Code Playgroud)
问题是,有时单元格的大小可能小于文本大小.我试过这个:
Excel.Range chartRange;
chartRange.EntireColumn.ColumnWidth = 31.43;
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我需要分别为每个列设置此属性.我怎么能这样做?
bto*_*rdz 37
我通常在vb中这样做,因为excel在vb中记录宏,因此更容易.所以我通常会去excel并保存我想做的宏.
这就是我现在所做的,我得到了这个代码:
Columns("E:E").ColumnWidth = 17.29;
Range("E3").Interior.Pattern = xlSolid;
Range("E3").Interior.PatternColorIndex = xlAutomatic;
Range("E3").Interior.Color = 65535;
Range("E3").Interior.TintAndShade = 0;
Range("E3").Interior.PatternTintAndShade = 0;
Run Code Online (Sandbox Code Playgroud)
我想你可以这样做:
xlWorkSheet.Columns[5].ColumnWidth = 18;
Run Code Online (Sandbox Code Playgroud)
对于您的上一个问题,您需要做的是循环通过您想要设置其宽度的列:
for (int i = 1; i <= 10; i++) // this will apply it from col 1 to 10
{
xlWorkSheet.Columns[i].ColumnWidth = 18;
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你
hak*_*kan 25
我是这样做的:
var xlApp = new Excel.Application();
var xlWorkBook = xlApp.Workbooks.Add(System.Reflection.Missing.Value);
var xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Item[1];
xlWorkSheet.Columns.AutoFit();
Run Code Online (Sandbox Code Playgroud)
通过这种方式,列始终适合单元格内的文本宽度.
希望对某人有所帮助!
小智 5
使用这个
((Excel.Range)oSheet.Cells[1, 1]).EntireColumn.ColumnWidth = 10;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84086 次 |
| 最近记录: |