using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using System.Diagnostics;
namespace zizhuTools
{
public partial class ghcrh : Form
{
public Excel.Application EApp;//声明公共对象变量
Excel.Range rng_bt;
Excel.Range rng_bw;
Stopwatch sw = new Stopwatch();
public ghcrh()
{
InitializeComponent();
}
private void ghcrh_Load(object sender, EventArgs e)
{
EApp = Globals.ThisAddIn.Application; //获得控制权
}
private void ghcrbt_btn_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
rng_bt = EApp.InputBox("选择单元格", "在Excel中选取", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
ghcrbt_tb.Text = rng_bt.get_Address();
this.WindowState = FormWindowState.Normal;
}
private void ghcrbt_tb_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
rng_bt = EApp.InputBox("选择单元格", "在Excel中选取", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
ghcrbt_tb.Text = rng_bt.get_Address();
this.WindowState = FormWindowState.Normal;
}
private void ghcrbw_tb_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
rng_bw = EApp.InputBox("选择单元格", "在Excel中选取", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
ghcrbw_tb.Text = rng_bw.get_Address();
this.WindowState = FormWindowState.Normal;
}
private void ghcrbw_btn_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
rng_bw = EApp.InputBox("选择单元格", "在Excel中选取", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 8);
ghcrbw_tb.Text = rng_bw.get_Address();
this.WindowState = FormWindowState.Normal;
}
private void button1_Click(object sender, EventArgs e)
{
sw.Start();
Excel.Worksheet sht = EApp.ActiveSheet;
string sht_name = "插入行结果表(" + EApp.Worksheets.Count + ")";
int rowsc = sht.UsedRange.Rows.Count; //4
int btcount =0;
int bwcount =0;
sht.Copy(Type.Missing, sht);
sht = EApp.ActiveSheet;
sht.Name = sht_name ;
//[1] [4]
//[5] [8]
//[9] [12]
//[13] [16]
//[17] [20]
//表头和表尾的差值=btcount+bwcount
int ghn = int.Parse(ghnum.Value.ToString());
//表头行数+间隔行数
if (ghcrbw_tb.Text != "")
{
bwcount = rng_bw.Rows.Count;
}
if (ghcrbt_tb.Text != "")
{
btcount = rng_bt.Rows.Count;
}
//表头和表尾都有
int colcz = btcount + bwcount + ghn;//4
int cz = ghn + btcount;
int cz2 = ghn + bwcount;
int bwcol = cz + 1;
int btcol = bwcol + 1;
int bwcol2 = ghn + 1;
if (bwcount > 0 && btcount > 0)
{
this.Text = "隔行插入行(程序运行中)";
decimal forcout1 = Math.Ceiling((decimal)(rowsc - colcz) / ghn);
int forcout = int.Parse(forcout1.ToString());
for (int i = 1; i <= forcout; i++)
{
rng_bw.EntireRow.Copy();
sht.Cells[bwcol, rng_bw.Column].Insert(Excel.XlInsertShiftDirection.xlShiftDown, Type.Missing);
rng_bt.EntireRow.Copy();
sht.Cells[btcol, rng_bt.Column].Insert(Excel.XlInsertShiftDirection.xlShiftDown, Type.Missing);
bwcol = btcol + cz;//8 12
btcol = btcol + colcz;//5 9
}
}
else if (btcount > 0)
{
this.Text = "隔行插入行(程序运行中)";
btcol = bwcol;
decimal forcout1 = Math.Ceiling((decimal)(rowsc - cz) / ghn);
int forcout = int.Parse(forcout1.ToString());
for (int i = 1; i <= forcout; i++)
{
rng_bt.EntireRow.Copy();
sht.Cells[btcol, rng_bt.Column].Insert(Excel.XlInsertShiftDirection.xlShiftDown, Type.Missing);
btcol = btcol + cz;//5 9
}
}
else if (bwcount > 0)
{
this.Text = "隔行插入行(程序运行中)";
btcol = cz2;
decimal forcout1 = Math.Ceiling((decimal)(rowsc - cz2) / ghn);
int forcout = int.Parse(forcout1.ToString());
for (int i = 1; i <= forcout; i++)
{
rng_bw.EntireRow.Copy();
sht.Cells[bwcol2, rng_bw.Column].Insert(Excel.XlInsertShiftDirection.xlShiftDown, Type.Missing);
bwcol2 = bwcol2 + cz2;//5 9
}
}
sw.Stop();
MessageBox.Show("程序运行完毕:" + sw.Elapsed, "完成");
this.Text = "隔行插入行";
this.TopMost = true;
this.TopMost = false;
}
private void button3_Click(object sender, EventArgs e)
{
string fileName = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Image\\原图\\生成资金发放条.png";
System.Diagnostics.Process.Start(fileName);
}
}
}