本文整理汇总了C#中Microsoft.Office.Interop.Excel.Application.get_Range方法的典型用法代码示例。如果您正苦于以下问题:C# Microsoft.Office.Interop.Excel.Application.get_Range方法的具体用法?C# Microsoft.Office.Interop.Excel.Application.get_Range怎么用?C# Microsoft.Office.Interop.Excel.Application.get_Range使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Office.Interop.Excel.Application
的用法示例。
在下文中一共展示了Microsoft.Office.Interop.Excel.Application.get_Range方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: backgroundWorkerMonCheCiExl_DoWork
private void backgroundWorkerMonCheCiExl_DoWork(object sender, DoWorkEventArgs e)
{
try
{
progressBarMonCheCi.Value = 60;
progressBarMonCheCi.Update();
labelProgMonCheCi.Text = "正在处理...";
labelProgMonCheCi.Update();
//建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks wbs = excel.Workbooks;//一个xls文档 new Microsoft.Office.Interop.Excel.Workbooks();
Microsoft.Office.Interop.Excel.Workbook wb = wbs.Add(true);// new Microsoft.Office.Interop.Excel.Workbook
Microsoft.Office.Interop.Excel.Worksheet ws;//excel中的一个sheet
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets["Sheet1"];
Microsoft.Office.Interop.Excel.Range merge_range = excel.get_Range(excel.Cells[1, 2], excel.Cells[1, 33]);
merge_range.Merge(Type.Missing);
excel.Cells[1, 1] = comboBox9.Text;
excel.Cells[1, 2] = comboBoxYear4.Text + "年" + comboBoxMon4.Text + "月份完成任务情况统计";
Microsoft.Office.Interop.Excel.Range bold_range = excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]);
bold_range.Font.Size = 16;
bold_range.Font.Bold = true;
bold_range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
bold_range.EntireColumn.AutoFit(); //自动调整列宽
bold_range.EntireRow.AutoFit();
bold_range = excel.get_Range(excel.Cells[1, 2], excel.Cells[1, 33]);
bold_range.Font.Size = 20;
bold_range.Font.Bold = true;
bold_range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
bold_range.EntireColumn.AutoFit(); //自动调整列宽
bold_range.EntireRow.AutoFit();
excel.Cells[2, 1] = "站名";
excel.Cells[2, 2] = "1";
excel.Cells[2, 3] = "2";
excel.Cells[2, 4] = "3";
excel.Cells[2, 5] = "4";
excel.Cells[2, 6] = "5";
excel.Cells[2, 7] = "6";
excel.Cells[2, 8] = "7";
excel.Cells[2, 9] = "8";
excel.Cells[2, 10] = "9";
excel.Cells[2, 11] = "10";
excel.Cells[2, 12] = "11";
excel.Cells[2, 13] = "12";
excel.Cells[2, 14] = "13";
excel.Cells[2, 15] = "14";
excel.Cells[2, 16] = "15";
excel.Cells[2, 17] = "16";
excel.Cells[2, 18] = "17";
excel.Cells[2, 19] = "18";
excel.Cells[2, 20] = "19";
excel.Cells[2, 21] = "20";
excel.Cells[2, 22] = "21";
excel.Cells[2, 23] = "22";
excel.Cells[2, 24] = "23";
excel.Cells[2, 25] = "24";
excel.Cells[2, 26] = "25";
excel.Cells[2, 27] = "26";
excel.Cells[2, 28] = "27";
excel.Cells[2, 29] = "28";
excel.Cells[2, 30] = "29";
excel.Cells[2, 31] = "30";
excel.Cells[2, 32] = "31";
excel.Cells[2, 33] = "合计";
excel.Cells[2, 34] = "实际";
excel.Cells[2, 35] = "差额";
//填充数据
for (int x = 0; x < dataGridViewMon.RowCount; x++)
{
for (int y = 0; y < dataGridViewMon.ColumnCount; y++)
{
if (dataGridViewMon[y, x].Value != null)
{
//MessageBox.Show(dataGridView1[y, x].Value.ToString());
excel.Cells[x + 3, y + 1] = dataGridViewMon[y, x].Value;
}
else
excel.Cells[x + 3, y + 1] = "";
}
}
Microsoft.Office.Interop.Excel.Range all_range = excel.get_Range(excel.Cells[2, 1], excel.Cells[1 + dataGridViewMon.RowCount, 35]);//现有的
all_range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
all_range.EntireColumn.AutoFit(); //自动调整列宽
all_range.EntireRow.AutoFit();
all_range.Borders.LineStyle = 1;
all_range.Font.Size = 12;
progressBarMonCheCi.Value =100;
progressBarMonCheCi.Update();
labelProgMonCheCi.Text = "正在处理...";
labelProgMonCheCi.Update();
wb.Saved = true;
wb.SaveCopyAs(fName); //保存
excel.Quit(); //关闭进程
//.........这里部分代码省略.........
示例2: timerMon5_Tick
private void timerMon5_Tick(object sender, EventArgs e)
{
if (flag_everydayexl == true)
{
this.dateTimePicker6.Enabled = false;
timerMon5.Stop();
dataGridViewMon.DataSource = crform_ds.Tables["Result_t0"];
//MessageBox.Show(dataGridView1[0, 0].Value.ToString());
//MessageBox.Show(dataGridView1[0, 1].Value.ToString());
//MessageBox.Show(dataGridView1[1, 0].Value.ToString());
//MessageBox.Show(dataGridView1[1, 1].Value.ToString());
//ExportExcel(dataGridView1);
try
{
if (dataGridViewMon.Rows.Count >= 1)
{
this.Enabled = false;
//MessageBox.Show("[email protected]@");
progressBarDay.Visible = true;
progressBarDay.Value = 0;
progressBarDay.Update();
labelProgDay.Text = "开始导出";
labelProgDay.Update();
string yr_str = comboBoxYear2.Text.Trim();
string mon_str = comboBoxMon2.Text.Trim();
string day_str = comboBoxDay2.Text.Trim();
string fName = "";
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = "d:";
saveFileDialog.Filter = "EXCEL文件|*.xlsx";
saveFileDialog.FilterIndex = 2;
saveFileDialog.FileName = mon_str + "月" + day_str + "日完成情况";
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
fName = saveFileDialog.FileName;
progressBarDay.Value = 12;
progressBarDay.Update();
this.Refresh();
//建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks wbs = excel.Workbooks;//一个xls文档 new Microsoft.Office.Interop.Excel.Workbooks();
Microsoft.Office.Interop.Excel.Workbook wb = wbs.Add(true);// new Microsoft.Office.Interop.Excel.Workbook
Microsoft.Office.Interop.Excel.Worksheet ws;//excel中的一个sheet
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets["Sheet1"];
//int num_sheet = dgv.Rows.Count / 55;
//label5.Text = "完成 " + progressBar1.Value.ToString() + "%";
//label5.Update();
//for (int cur_sheet = 1; cur_sheet <= num_sheet; cur_sheet++)
//{
// if (num_sheet != 1)
// {
// progressBar1.Value = Convert.ToInt32(12 + cur_sheet * 87 / num_sheet);
// progressBar1.Update();
//label5.Text = "完成 " + progressBar1.Value.ToString() + "%";
//label5.Update();
// }
// else
// {
// progressBar1.Value = 50;
// progressBar1.Update();
//label5.Text = "完成 " + progressBar1.Value.ToString() + "%";
//label5.Update();
// }
// if (cur_sheet == 1)
// ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets["Sheet1"];
// else
// ws = wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing) as Microsoft.Office.Interop.Excel.Worksheet;
// ws.Name = cur_sheet.ToString();
excel.Cells[2, 1] = "一班:王玉宝";
excel.Cells[2, 6] = "二班:蔡震";
excel.Cells[2, 11] = "三班:韩培元";
excel.Cells[2, 13] = "日期:" + mon_str + "月" + day_str + "日";
Microsoft.Office.Interop.Excel.Range merge_range = excel.get_Range(excel.Cells[2, 13], excel.Cells[2, 14]);
merge_range.Merge(Type.Missing);
Microsoft.Office.Interop.Excel.Range bold_range = excel.get_Range(excel.Cells[2, 1], excel.Cells[2, 15]);
bold_range.Font.Bold = true;
//生成字段名称
excel.Cells[3, 1] = "楼名";
excel.Cells[3, 2] = "日产箱数";
excel.Cells[3, 3] = "实际吨数";
excel.Cells[3, 4] = "平均吨数";
excel.Cells[3, 6] = "楼名";
excel.Cells[3, 7] = "日产箱数";
excel.Cells[3, 8] = "实际吨数";
excel.Cells[3, 9] = "平均吨数";
excel.Cells[3, 11] = "楼名";
excel.Cells[3, 12] = "日产箱数";
excel.Cells[3, 13] = "实际吨数";
excel.Cells[3, 14] = "平均吨数";
//.........这里部分代码省略.........
示例3: backgroundWorkerYearExl_DoWork
private void backgroundWorkerYearExl_DoWork(object sender, DoWorkEventArgs e)
{
try
{
progressBarYear.Value = 10;
progressBarYear.Update();
labelProgYear.Text = "完成 " + progressBarYear.Value.ToString() + "%";
labelProgYear.Update();
string yr_str = comboBoxYear3.Text.Substring(2, 2);
//建立Excel对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks wbs = excel.Workbooks;//一个xls文档 new Microsoft.Office.Interop.Excel.Workbooks();
Microsoft.Office.Interop.Excel.Workbook wb = wbs.Add(true);// new Microsoft.Office.Interop.Excel.Workbook
Microsoft.Office.Interop.Excel.Worksheet ws;//excel中的一个sheet
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets["Sheet1"];
ws.Name = yr_str + "年总合计";
#region 年总计
Microsoft.Office.Interop.Excel.Range merge_range = excel.get_Range(excel.Cells[1, 1], excel.Cells[2, 11]);
merge_range.Merge(Type.Missing);
merge_range = excel.get_Range(excel.Cells[3, 1], excel.Cells[4, 1]);
merge_range.Merge(Type.Missing);
merge_range = excel.get_Range(excel.Cells[3, 2], excel.Cells[4, 2]);
merge_range.Merge(Type.Missing);
merge_range = excel.get_Range(excel.Cells[3, 3], excel.Cells[4, 3]);
merge_range.Merge(Type.Missing);
merge_range = excel.get_Range(excel.Cells[3, 10], excel.Cells[4, 10]);
merge_range.Merge(Type.Missing);
merge_range = excel.get_Range(excel.Cells[3, 11], excel.Cells[4, 11]);
merge_range.Merge(Type.Missing);
merge_range = excel.get_Range(excel.Cells[3, 4], excel.Cells[3, 6]);
merge_range.Merge(Type.Missing);
merge_range = excel.get_Range(excel.Cells[3, 7], excel.Cells[3, 9]);
merge_range.Merge(Type.Missing);
excel.Cells[1, 1] = comboBoxYear3.Text +"年垃圾产量总计表";
Microsoft.Office.Interop.Excel.Range bold_range = excel.get_Range(excel.Cells[1, 1], excel.Cells[2,11]);
bold_range.Font.Size = 20;
bold_range.Font.Bold = true;
bold_range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
bold_range.EntireColumn.AutoFit(); //自动调整列宽
bold_range.EntireRow.AutoFit();
excel.Cells[3, 1] = "月份";
excel.Cells[3, 2] = "月产箱数";
excel.Cells[3, 3] = "实际吨数";
excel.Cells[3, 4] = "自运箱数";
excel.Cells[3, 5] = "实际吨数";
excel.Cells[3, 10] = "合计箱数";
excel.Cells[3, 11] = "合计吨数";
excel.Cells[4, 4] = "西清";
excel.Cells[4, 5] = "十队";
excel.Cells[4, 6] = "合计";
excel.Cells[4, 7] = "西清";
excel.Cells[4, 8] = "十队";
excel.Cells[4, 9] = "合计";
//填充数据
for (int x = 0; x < dataGridViewMon.RowCount; x++)
{
for (int y = 0; y < dataGridViewMon.ColumnCount; y++)
{
if (dataGridViewMon[y, x].Value != null)
{
//MessageBox.Show(dataGridView1[y, x].Value.ToString());
excel.Cells[x + 5, y + 1] = dataGridViewMon[y, x].Value;
}
else
excel.Cells[x + 5, y + 1] = "";
}
}
Microsoft.Office.Interop.Excel.Range all_range = excel.get_Range(excel.Cells[3, 1], excel.Cells[17, 11]);//现有的
all_range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
all_range.EntireColumn.AutoFit(); //自动调整列宽
all_range.EntireRow.AutoFit();
all_range.Borders.LineStyle = 1;
all_range.Font.Size = 12;
#endregion
progressBarYear.Value = 20;
progressBarYear.Update();
labelProgYear.Text = "完成 " + progressBarYear.Value.ToString() + "%";
labelProgYear.Update();
dataGridViewMon.DataSource = crform_ds.Tables["MonOutPut"];
for (int cur_sheet = 1; cur_sheet <= 12; cur_sheet++)
{
progressBarYear.Value = Convert.ToInt32(20 + 6.5*cur_sheet);
progressBarYear.Update();
labelProgYear.Text = "完成 " + progressBarYear.Value.ToString() + "%";
labelProgYear.Update();
ws = wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing) as Microsoft.Office.Interop.Excel.Worksheet;
ws.Name = yr_str + "年" + cur_sheet.ToString()+"月";
//.........这里部分代码省略.........
示例4: ExportExcel
//.........这里部分代码省略.........
excel.Cells[1, 1] = " 日期\n名称";
excel.Cells[1, 2] = "1";
excel.Cells[1, 3] = "2";
excel.Cells[1, 4] = "3";
excel.Cells[1, 5] = "4";
excel.Cells[1, 6] = "5";
excel.Cells[1, 7] = "6";
excel.Cells[1, 8] = "7";
excel.Cells[1, 9] = "8";
excel.Cells[1, 10] = "9";
excel.Cells[1, 11] = "10";
excel.Cells[1, 12] = "11";
excel.Cells[1, 13] = "12";
excel.Cells[1, 14] = "13";
excel.Cells[1, 15] = "14";
excel.Cells[1, 16] = "15";
excel.Cells[1, 17] = "合计";
excel.Cells[1, 18] = "";
excel.Cells[2, 1] = "";
excel.Cells[2, 2] = "箱数";
excel.Cells[2, 3] = "吨数";
excel.Cells[2, 4] = "吨数";
excel.Cells[2, 5] = "吨数";
excel.Cells[2, 6] = "吨数";
excel.Cells[2, 7] = "吨数";
excel.Cells[2, 8] = "吨数";
excel.Cells[2, 9] = "吨数";
excel.Cells[2, 10] = "吨数";
excel.Cells[2, 11] = "吨数";
excel.Cells[2, 12] = "吨数";
excel.Cells[2, 13] = "吨数";
excel.Cells[2, 14] = "吨数";
excel.Cells[2, 15] = "吨数";
excel.Cells[2, 16] = "吨数";
excel.Cells[2, 17] = "吨数";
excel.Cells[2, 18] = "总箱数";
Microsoft.Office.Interop.Excel.Range merge_range = excel.get_Range(excel.Cells[1, 17], excel.Cells[1, 18]);
merge_range.Merge(Type.Missing);
Microsoft.Office.Interop.Excel.Range merge_range2 = excel.get_Range(excel.Cells[1, 1], excel.Cells[2, 1]);
merge_range2.Merge(Type.Missing);
//填充数据
for (int i = (cur_sheet - 1) * 56 + 1; i < (cur_sheet - 1) * 56 + 57; i++)
{
for (int j = 0; j < dgv.ColumnCount - 1; j++)
{
if (dgv[j, i - 1].Value != null)
{
excel.Cells[i - (cur_sheet - 1) * 56 + 2, j + 1] = dgv[j, i - 1].Value;
}
else
excel.Cells[i - (cur_sheet - 1) * 56 + 2, j + 1] = "";
}
}
Microsoft.Office.Interop.Excel.Range all_range = excel.get_Range(excel.Cells[1, 1], excel.Cells[58, 18]);//现有的
all_range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
Microsoft.Office.Interop.Excel.Range c11_range = excel.get_Range(excel.Cells[1, 1], excel.Cells[1, 1]);
c11_range.HorizontalAlignment = XlHAlign.xlHAlignLeft;
all_range.EntireColumn.AutoFit(); //自动调整列宽
all_range.EntireRow.AutoFit();
all_range.Borders.LineStyle = 1;
labelProgMon.Text = "完成 " + progressBarMon.Value.ToString() + "%";
labelProgMon.Update();
}//end for每个sheet
progressBarMon.Value = progressBarMon.Maximum;
progressBarMon.Update();
wb.Saved = true;
wb.SaveCopyAs(fName); //保存
excel.Quit(); //关闭进程
labelProgMon.Text = "已完成";
labelProgMon.Update();
MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
//label5.Text = "完成";
toolStripButtonMonExl.Enabled = false;
}
else
{
progressBarMon.Visible = false;
progressBarMon.Update();
labelProgMon.Text = "";
labelProgMon.Update();
MessageBox.Show("未导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
}
}
}
catch (Exception ex)
{
progressBarMon.Visible = false;
progressBarMon.Update();
labelProgMon.Text = "";
labelProgMon.Update();
MessageBox.Show(ex.Message.ToString());
}
//*/
}
示例5: CreateExcel
/// <summary>根据参数创建excel表</summary>
public void CreateExcel(System.Data.DataTable dt, string pathname, string[] str)
{
int eRowIndex = 2;
int eColIndex = 1;
int cols = dt.Columns.Count;
int rows = dt.Rows.Count;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.Worksheets["sheet1"];
//xlSheet.PageSetup.CenterHorizontally = false;//页面水平居中//xlSheet.PageSetup.CenterVertically = false;//页面不垂直居中
xlApp.Cells[1, cols] = str[0];
xlApp.get_Range((object)xlApp.Cells[1, 1], (object)xlApp.Cells[1, cols]).Font.Bold = true;
xlApp.get_Range((object)xlApp.Cells[1, 1], (object)xlApp.Cells[1, cols]).Font.Size = str[1];
xlApp.get_Range((object)xlApp.Cells[1, 1], (object)xlApp.Cells[1, cols]).MergeCells = true;
xlApp.get_Range((object)xlApp.Cells[1, 1], (object)xlApp.Cells[1, cols]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
try
{
//列名的处理
for (int i = 0; i < cols; i++)
{
xlApp.Cells[eRowIndex, eColIndex] = dt.Columns[i].ColumnName;
eColIndex++;
}
//列内容样式
xlApp.get_Range((object)xlApp.Cells[eRowIndex, 1], (object)xlApp.Cells[eRowIndex, cols]).Font.Bold = true;
xlApp.get_Range((object)xlApp.Cells[eRowIndex, 1], (object)xlApp.Cells[eRowIndex, cols]).Font.Name = str[3];
xlApp.get_Range((object)xlApp.Cells[eRowIndex, 1], (object)xlApp.Cells[eRowIndex, cols]).Font.Size = str[2];
//xlApp.get_Range((object)xlApp.Cells[eRowIndex, 1], (object)xlApp.Cells[eRowIndex, 1]).ColumnWidth = 20;//设置列宽
eRowIndex++;
for (int i = 0; i < rows; i++)
{
eColIndex = 1;
for (int j = 0; j < cols; j++)
{
xlApp.Cells[eRowIndex, eColIndex] = dt.Rows[i][j].ToString();
eColIndex++;
}
eRowIndex++;
}
Microsoft.Office.Interop.Excel.Range range1 = (Microsoft.Office.Interop.Excel.Range)xlApp.get_Range((object)xlApp.Cells[1, 1], (object)xlApp.Cells[eRowIndex - 1, cols]);
range1.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
range1.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
range1.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
range1.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
range1.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
//xlApp.Visible = true;
xlApp.Cells[eRowIndex + 1, 1] = "附加信息:";
xlApp.get_Range((object)xlApp.Cells[eRowIndex + 1, 1], (object)xlApp.Cells[eRowIndex + 1, cols]).MergeCells = true;
xlApp.get_Range((object)xlApp.Cells[eRowIndex + 1, 1], (object)xlApp.Cells[eRowIndex + 1, cols]).MergeCells = true;
xlApp.get_Range((object)xlApp.Cells[eRowIndex + 1, 1], (object)xlApp.Cells[eRowIndex + 1, cols]).RowHeight = str[4];
xlApp.Cells[eRowIndex + 2, 1] = "确认签名:";
xlApp.get_Range((object)xlApp.Cells[eRowIndex + 1, 1], (object)xlApp.Cells[eRowIndex + 1, cols]).MergeCells = true;
xlApp.get_Range((object)xlApp.Cells[eRowIndex + 1, 1], (object)xlApp.Cells[eRowIndex + 1, cols]).MergeCells = true;
//行、列自适应
xlApp.Cells.EntireColumn.AutoFit(); //xlApp.Cells.EntireRow.AutoFit(); //xlApp.DisplayAlerts = true; //xlBook.SaveCopyAs(path);
xlSheet.SaveAs(pathname);
xlApp.Workbooks.Close();
}
catch (Exception e)
{
Response.Write("<script>alert('0000x1:'" + e.Message + ")</script>");
}
finally
{
xlApp.Quit();
GC.Collect(); //杀掉Excel进程。
}
}