本文整理汇总了C#中Microsoft.Office.Interop.Excel.ApplicationClass.get_Range方法的典型用法代码示例。如果您正苦于以下问题:C# ApplicationClass.get_Range方法的具体用法?C# ApplicationClass.get_Range怎么用?C# ApplicationClass.get_Range使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Office.Interop.Excel.ApplicationClass
的用法示例。
在下文中一共展示了ApplicationClass.get_Range方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: btnExport_Click
private void btnExport_Click(object sender, EventArgs e)
{
Excel.ApplicationClass excelSheet = new Excel.ApplicationClass();
excelSheet.Application.Workbooks.Add(true);
int columnIndex = 0;
foreach (DataGridViewColumn column in dgridResult.Columns)
{
if (column.HeaderText.Trim() != "" && column.Visible == true)
{
columnIndex++;
excelSheet.Cells[1, columnIndex] = column.HeaderText;
}
}
int rowIndex = 0;
foreach (DataGridViewRow row in dgridResult.Rows)
{
rowIndex++;
columnIndex = 0;
foreach (DataGridViewColumn column in dgridResult.Columns)
{
if (column.HeaderText.Trim() != "" && column.Visible == true)
{
columnIndex++;
excelSheet.Cells[rowIndex + 1, columnIndex] = row.Cells[column.Name].FormattedValue;
}
}
}
excelSheet.Visible = true;
Excel.Worksheet workSheet = (Excel.Worksheet)excelSheet.ActiveSheet;
Excel.Range titleRange = excelSheet.get_Range(workSheet.Cells[1, 1], workSheet.Cells[1, columnIndex]);
titleRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
}
示例2: ExportDataTableToExcel
/// <summary>
/// 报表导出excel共通函数(适用于DataTable绑定DataGridView)
/// </summary>
/// <param name="Grid">数据源</param>
/// <param name="ReportTitle">报表名</param>
/// <param name="DPTMessage">信息栏信息</param>
public static void ExportDataTableToExcel(System.Data.DataTable dbSource, string[] header,string ReportTitle, string DPTMessage)
{
System.Data.DataTable myTable = dbSource;
try
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
//定义起始行,列
int rowIndex;
int colIndex;
rowIndex = 2;
colIndex = 0;
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, myTable.Columns.Count]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = ReportTitle;
xlApp.ActiveCell.Font.Size = 14;
xlApp.ActiveCell.Font.Bold = true;
//设置禁止弹出保存和覆盖的询问提示框
xlApp.DisplayAlerts = true;
//将表中的栏位名称填到Excel的第一行
//foreach (DataColumn Col in myTable.Columns)
//{
// colIndex = colIndex + 1;
// xlApp.Cells[2, colIndex] = Col.ColumnName;
//}
for (int i = 0; i < header.Length; i++)
{
colIndex = colIndex + 1;
xlApp.Cells[2, colIndex] = header[i].ToString();
}
//得到的表所有行,赋值给单元格
for (int row = 0; row < myTable.Rows.Count; row++)
{
rowIndex = rowIndex + 1;
colIndex = 0;
for (int col = 0; col < myTable.Columns.Count; col++)
{
colIndex = colIndex + 1;
xlApp.Cells[rowIndex, colIndex] = dbSource.Rows[row][col].ToString();
}
}
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).Font.Bold = true;
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowIndex, colIndex]).Borders.LineStyle = 1;
xlApp.Cells.EntireColumn.AutoFit();
xlApp.Cells.VerticalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
xlApp.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
xlApp.Visible = true;
//
//加载一个合计行
//
int rowSum = rowIndex;
Microsoft.Office.Interop.Excel.Range Totle = xlApp.get_Range(xlApp.Cells[rowSum + 1, 1], xlApp.Cells[rowSum + 1, dbSource.Columns.Count]);
Totle.MergeCells = true;
xlApp.Cells[rowSum + 1, 1] = DPTMessage;
xlApp.ActiveCell.Font.Bold = true;
//字体
xlApp.get_Range(xlApp.Cells[rowSum + 1, 1], xlApp.Cells[rowSum + 1, 1]).Font.Size = 10;
//对齐方式
xlApp.get_Range(xlApp.Cells[rowSum + 1, 1], xlApp.Cells[rowSum + 1, 1]).HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlLeft;
//
//绘制边框
//
xlApp.get_Range(xlApp.Cells[3, 1], xlApp.Cells[rowSum, colIndex]).Borders.LineStyle = 1;
xlApp.get_Range(xlApp.Cells[3, 1], xlApp.Cells[rowSum, 2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThin;//设置左边线加粗
xlApp.get_Range(xlApp.Cells[3, 1], xlApp.Cells[4, colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThin;//设置上边线加粗
xlApp.get_Range(xlApp.Cells[3, colIndex], xlApp.Cells[rowSum, colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThin;//设置右边线加粗
xlApp.get_Range(xlApp.Cells[rowSum, 1], xlApp.Cells[rowSum, colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThin;//设置下边线加粗
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
GC.Collect();
}
catch (Exception e)
{
throw e;
}
}
示例3: ExportToExcelNullMerge
/**/
/// <summary>
/// ��DataGridView�е����ݵ�����Excel�У���������ʾ����(����ģ��)
/// ֻ�����кϲ���ĵ�������������˵�ĺϲ�����ָ����е�Ԫ���ֵΪ��ʱ�Զ�����һ�еĵ�λ����кϲ�
/// </summary>
/// <param name="caption">Ҫ��ʾ��ҳͷ</param>
/// <param name="date">��ӡ����</param>
/// <param name="dgv">Ҫ���е�����DataGridView</param>
/// <param name="MergeCount">�ϲ�������,û���õ�</param>
public void ExportToExcelNullMerge(string caption, string date, DataGridView dgv, int MergeCount)
{
//DataGridView�ɼ�����
int visiblecolumncount = 0;
for (int i = 0; i < dgv.Columns.Count; i++)
{
if (dgv.Columns[i].Visible == true && (dgv.Columns[i] is DataGridViewTextBoxColumn))
{
visiblecolumncount++;
}
}
try
{
//��ǰ�����е�����
int currentcolumnindex = 1;
//��ǰ�����е�����
int currentrowindex = 4;
Microsoft.Office.Interop.Excel.ApplicationClass Mylxls = new Microsoft.Office.Interop.Excel.ApplicationClass();
Mylxls.Application.Workbooks.Add(true);
//Mylxls.Cells.Font.Size = 10.5; //����Ĭ�������С
//���ñ�ͷ
Mylxls.Caption = caption;
//��ʾ��ͷ
Mylxls.Cells[1, 1] = caption;
//��ʾʱ��
Mylxls.Cells[2, 1] = date;
for (int i = 0; i < dgv.Columns.Count; i++)
{
if (dgv.Columns[i].Visible == true && (dgv.Columns[i] is DataGridViewTextBoxColumn)) //�����ʾ
{
Mylxls.Cells[3, currentcolumnindex] = dgv.Columns[i].HeaderText;
Mylxls.get_Range(Mylxls.Cells[3, currentcolumnindex], Mylxls.Cells[3, currentcolumnindex]).Cells.Borders.LineStyle = 1; //���ñ߿�
//Mylxls.get_Range(Mylxls.Cells[3, currentcolumnindex], Mylxls.Cells[3, currentcolumnindex]).Font.Bold = true; //����
Mylxls.get_Range(Mylxls.Cells[3, currentcolumnindex], Mylxls.Cells[3, currentcolumnindex]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //������ʾ
currentcolumnindex++;
}
}
Mylxls.get_Range(Mylxls.Cells[1, 1], Mylxls.Cells[1, visiblecolumncount]).MergeCells = true; //�ϲ���Ԫ��
Mylxls.get_Range(Mylxls.Cells[1, 1], Mylxls.Cells[1, 1]).RowHeight = 30; //�и�
//Mylxls.get_Range(Mylxls.Cells[1, 1], Mylxls.Cells[1, 1]).Font.Name = "����";
//Mylxls.get_Range(Mylxls.Cells[1, 1], Mylxls.Cells[1, 1]).Font.Size = 14; //�����С
Mylxls.get_Range(Mylxls.Cells[1, 1], Mylxls.Cells[1, visiblecolumncount]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //������ʾ
Mylxls.get_Range(Mylxls.Cells[2, 1], Mylxls.Cells[2, 2]).MergeCells = true; //�ϲ�
Mylxls.get_Range(Mylxls.Cells[2, 1], Mylxls.Cells[2, 2]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //�����ʾ
//Mylxls.get_Range(Mylxls.Cells[1, 2], Mylxls.Cells[1, 2]).ColumnWidth = 12; //��
for (int i = 0; i < dgv.Rows.Count; i++)
{
currentcolumnindex = 1;
currentrowindex = 4 + i;
for (int j = 0; j < dgv.Columns.Count; j++)
{
if (dgv.Columns[j].Visible == true && (dgv.Columns[j] is DataGridViewTextBoxColumn))
{
if (dgv[j, i].Value != null) //�����Ԫ�����ݲ�Ϊ��
{
Mylxls.Cells[currentrowindex, currentcolumnindex] = dgv[j, i].Value.ToString();
}
Mylxls.get_Range(Mylxls.Cells[1, currentcolumnindex], Mylxls.Cells[1, currentcolumnindex]).ColumnWidth = dgv.Columns[j].Width / 8;
Mylxls.get_Range(Mylxls.Cells[currentrowindex, currentcolumnindex], Mylxls.Cells[currentrowindex, currentcolumnindex]).Cells.Borders.LineStyle = 1; //���ñ߿�
if (dgv.Rows[i].Cells[j].Value.ToString() == "")
{
Mylxls.get_Range(Mylxls.Cells[currentrowindex - 1, currentcolumnindex], Mylxls.Cells[currentrowindex, currentcolumnindex]).MergeCells = true;
//Mylxls.get_Range(Mylxls.Cells[i - 1, col + 2], Mylxls.Cells[rowIndex, col + 2]).MergeCells = true;
//Mylxls.get_Range(Mylxls.Cells[i - 1, col + 3], Mylxls.Cells[rowIndex, col + 3]).MergeCells = true;
}
currentcolumnindex++;
}
}
}
Mylxls.Visible = true;
}
catch
{
MessageBox.Show("��Ϣ����ʧ�ܣ���ȷ����Ļ�����װ��Microsoft Office Excel 2003��", "����", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
}
}
/**/
/// <summary>
/// ��DataGridView�е����ݵ�����Excel�У���������ʾ����(����ģ��)
/// ֻ�����кϲ���ĵ���,���ҿɸ���ǰ���DataGridViewCheckBoxColumn�Ƿ�ѡ����е���
/// </summary>
/// <param name="caption">Ҫ��ʾ��ҳͷ</param>
//.........这里部分代码省略.........