private void 打开数据toolStripMenuItem2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel文件|*.xlsx;*.xls";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string str = openFileDialog.FileName;
Excel.Application xlApp = new Excel.Application();
Excel.Workbook workbook = xlApp.Application.Workbooks.Add(str);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
Excel.Range ran = (Excel.Range)worksheet.Cells[1, 2];
if (ran.Value2 == null)
{
Form_Main.frm.uitxt_ybbh.Text = "";
}
else
{
Form_Main.frm.uitxt_ybbh.Text = ran.Value2.ToString();
}
ran = (Excel.Range)worksheet.Cells[2, 2];
if (ran.Value2 == null)
{
Form_Main.frm.uitxt_jxbh.Text = "";
}
else
{
Form_Main.frm.uitxt_jxbh.Text = ran.Value2.ToString();
}
ran = (Excel.Range)worksheet.Cells[3, 2];
if (ran.Value2 == null)
{
Form_Main.frm.uitxt_cgqbh.Text = "";
}
else
{
Form_Main.frm.uitxt_cgqbh.Text = ran.Value2.ToString();
}
ran = (Excel.Range)worksheet.Cells[4, 2];
if (ran.Value2 == null)
{
Form_Main.frm.uitxt_gsmc.Text = "";
}
else
{
Form_Main.frm.uitxt_gsmc.Text = ran.Value2.ToString();
}
ran = (Excel.Range)worksheet.Cells[5, 2];
if (ran.Value2 == null)
{
Form_Main.frm.uitxt_bz.Text = "";
}
else
{
Form_Main.frm.uitxt_bz.Text = ran.Value2.ToString();
}
ran = (Excel.Range)worksheet.Cells[6, 2];
int a = Convert.ToInt32(ran.Value2.ToString());
for (int i = 8; i <= 7 + a; i++)
{
Excel.Range ran1 = (Excel.Range)worksheet.Cells[i, 1];
Excel.Range ran2 = (Excel.Range)worksheet.Cells[i, 2];
Excel.Range ran3 = (Excel.Range)worksheet.Cells[i, 3];
Excel.Range ran4 = (Excel.Range)worksheet.Cells[i, 4];
DataGridViewRow row = new DataGridViewRow();//创建一个新的数据行
DataGridViewCell cell = new DataGridViewTextBoxCell();//创建新行的单元格并设置他们的值
cell.Value = Form_Main.frm.hsl_bzbssll.DisplayText;
row.Cells.Add(cell);
DataGridViewCell cell2 = new DataGridViewTextBoxCell();
cell2.Value = Form_Main.frm.hsl_bjbssll.DisplayText;
row.Cells.Add(cell2);
DataGridViewCell cell3 = new DataGridViewTextBoxCell();
cell3.Value = Form_Main.frm.hsl_jdwc.DisplayText;
row.Cells.Add(cell3);
DataGridViewCell cell4 = new DataGridViewTextBoxCell();
cell4.Value = Form_Main.frm.hsl_ybxs.DisplayText;
row.Cells.Add(cell4);
Form_shujuzaiji.shujuzaiji.dataGridView1.Rows.Add(row);//将新行添加到dataGridView1的Rows集合中
}
for (int i = 0; i < Form_shujuzaiji.shujuzaiji.dataGridView1.Rows.Count; i++)
{
if (i % 2 == 0)
{
Form_shujuzaiji.shujuzaiji.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightYellow;
}
}
Form_shujuzaiji.shujuzaiji.dataGridView1.Rows[Form_shujuzaiji.shujuzaiji.dataGridView1.Rows.Count - 1].Selected = true; // 选中新行
Form_shujuzaiji.shujuzaiji.dataGridView1.CurrentCell = Form_shujuzaiji.shujuzaiji.dataGridView1.Rows[Form_shujuzaiji.shujuzaiji.dataGridView1.Rows.Count - 1].Cells[0];
显示行
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)//显示行号
{
var grid = sender as DataGridView;
var rowIdx = (e.RowIndex + 1).ToString();
var centerFormat = new StringFormat()
{
// right alignment might actually make more sense for numbers
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
}