在编程的过程中,特别是AE二次开发或office二次开发的过程中,在调用已有函数时,需要传入较多的参数,如在excel二次开发时,对一个单元格进行操作,需要传入字体信息、边框样式、内容等。
结构体如下:
/// <summary> /// 报表列头信息 /// </summary> public struct ExcelColumnHead { /// <summary> /// 起始单元格行索引 /// </summary> public int StartRow; /// <summary> /// 起始单元格列索引 /// </summary> public int StartColumn; /// <summary> /// 行数 /// </summary> public int RowCount; /// <summary> /// 列数 /// </summary> public int ColumnCount; /// <summary> /// 字体大小 /// </summary> public int FontSize; /// <summary> /// 字体名称 /// </summary> public string FontName; /// <summary> /// 是否粗体 /// </summary> public bool IsBold; public ExcelColumnHead(int pStartRow, int pStartColumn, int pRowCount, int pColumnCount, int pFontSize, string pFontName , bool pIsBold) { StartRow = pStartRow; StartColumn = pStartColumn; RowCount = pRowCount; ColumnCount = pColumnCount; FontSize = pFontSize; FontName = pFontName; IsBold = pIsBold; } }
调用代码:
/// <summary> /// 向表格中写入内容 /// </summary> /// <param name="pColumnHeadInfo"></param> /// <param name="excelValue"></param> /// <param name="_Worksheet"></param> private void CreateExcelHead(ExcelColumnHead pColumnHeadInfo, string excelValue, ref Excel.Worksheet _Worksheet) { Excel.Range _Range = null; object point1 = _Worksheet.Cells[pColumnHeadInfo.StartRow, pColumnHeadInfo.StartColumn]; object point2 = _Worksheet.Cells[pColumnHeadInfo.StartRow + pColumnHeadInfo.RowCount - 1, pColumnHeadInfo.StartColumn + pColumnHeadInfo.ColumnCount - 1]; _Range = _Worksheet.get_Range(point1, point2);//获取单元格 if (pColumnHeadInfo.RowCount > 0 || pColumnHeadInfo.ColumnCount > 0) { _Range.MergeCells = true; //合并单元格 } _Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//内容水平居中 _Range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//内容垂直居中 _Range.Font.Size = pColumnHeadInfo.FontSize; _Range.Font.Name = pColumnHeadInfo.FontName; _Range.Font.Bold = pColumnHeadInfo.IsBold; _Worksheet.Cells[_Range.Row, _Range.Column] = excelValue;//把内容写入单元格 _Range.EntireColumn.AutoFit(); if (pColumnHeadInfo.StartRow != 1 && pColumnHeadInfo.StartRow != 2)//不是标题的单元格加边框 { SetRangRoundLine(_Range); } }
注:此随笔由本人与RoyYu (http://home.cnblogs.com/u/RoyYu/)共同完成