您的位置:首页 > 编程语言 > C#

C#操作Excel数据导入和dataGridView重绘

2015-05-20 22:29 495 查看
一 C# 操作excel导入和datagridviewview重绘

代码部分:

(1) 判断本机是否存在excel;

/// <summary>

/// 判断本机是否有excel

/// </summary>

/// <returns></returns>

private bool codeboolisExcelInstalled()

{

Type type = Type.GetTypeFromProgID("Excel.Application");

return type != null;

}

(2) 找出需要导入的excel表中的相应sheet,不支持多个sheet的导入;

/// <summary>

/// 获取要导入的excel中的sheet表名称,这里支持sheet表名的重命名

/// </summary>

/// <param name="filePath"></param>

/// <returns></returns>

private static string[] GetExcelSheetNames(string filePath)

{

Microsoft.Office.Interop.Excel.ApplicationClass excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

Microsoft.Office.Interop.Excel.Workbooks wbs = excelApp.Workbooks;

Microsoft.Office.Interop.Excel.Workbook wb = wbs.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);

int count = wb.Worksheets.Count;

string[] names = new string[count];

for (int i = 1; i <= count; i++)

{

names[i - 1] = ((Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[i]).Name;

}

wb.Close(null, null, null);

excelApp.Quit();

wbs.Close();

System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);

System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);

System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);

excelApp = null;

wbs = null;

wb = null;

return names;

}

(3) 导入操作代码;

/// <summary>

/// 导入Excel计划表

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button2_Click(object sender, EventArgs e)

{

try

{

//打开要导入的文件

OpenFileDialog open = new OpenFileDialog();

if (open.ShowDialog() == DialogResult.OK)

{

string fileName = open.FileName;

ReadExcel(fileName);

label13.Visible = true;

label13.Text = textBox1.Text.ToString() + "鉴定计划表";

fileName = "";

return;

}

else

{

return;

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

#region 考评员计划表中的考评员所在的列

int[] sum1 = new int[5] { 7, 8, 9, 10, 11 };

int[] sum2 = new int[5] { 15, 16, 17, 18, 19 };

int[] sum3 = new int[5] { 23, 24, 25, 26, 27 };

int[] sum4 = new int[5] { 31, 32, 33, 34, 35 };

int[] sum5 = new int[5] { 39, 40, 41, 42, 43 };

int[] sum6 = new int[5] { 47, 48, 49, 50, 51 };

int[] sum7 = new int[5] { 55, 56, 57, 58, 59 };

int[] sum8 = new int[5] { 63, 64, 65, 66, 67 };

int[] sum9 = new int[5] { 71, 72, 73, 74, 75 };

int[] sum10 = new int[5] { 79, 80, 81, 82, 83 };

#endregion

string[] titleName1 = new string[10];//记录Excel表中考核题目信息

int abc = 0;

private void ReadExcel(string sPath)

{

//dataGridView1.Columns.Clear();

//HDR = YES 代表第一行是标题,不做为数据使用;如果用HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES

//参数Excel 8.0 对于Excel 97以上到2003版本都用Excel 8.0,2007或2010的都用Extended Properties=Excel 12.0

//当 IMEX=0 时为“汇出模式”,这个模式开启的 Excel 档案只能用来做“写入”用途。1 是读取 2 是写入和读取

try

{

#region 导入计划表,重绘显示

if (codeboolisExcelInstalled())

{

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sPath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";

//string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";

excelConnection = new OleDbConnection(connectionString);

excelConnection.Open();

string SheetName = GetExcelSheetNames(sPath)[0]; ;

string strSQL = "SELECT * FROM [" + SheetName + "$]";

OleDbCommand dbCommand = new OleDbCommand(strSQL, excelConnection);

OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);

System.Data.DataTable dTable = new System.Data.DataTable();

dataAdapter.Fill(dTable);

if (dTable.Rows.Count != 0)

{

abc = dTable.Rows.Count;

#region 选获取考评员计划表中的题目信息

titleName1[0] = dTable.Rows[0][6].ToString();

titleName1[1] = dTable.Rows[0][14].ToString();

titleName1[2] = dTable.Rows[0][22].ToString();

titleName1[3] = dTable.Rows[0][30].ToString();

titleName1[4] = dTable.Rows[0][38].ToString();

titleName1[5] = dTable.Rows[0][46].ToString();

titleName1[6] = dTable.Rows[0][54].ToString();

titleName1[7] = dTable.Rows[0][62].ToString();

titleName1[8] = dTable.Rows[0][70].ToString();

titleName1[9] = dTable.Rows[0][78].ToString();

#endregion

#region 获得excel考评员计划表后在进行表格重绘

dataGridView2.Visible = true;

for (int k = 0; k < dataGridView2.Columns.Count; k++)

{

dataGridView2.Columns[k].SortMode = DataGridViewColumnSortMode.NotSortable;

}

dataGridView2.Rows.Clear();

#endregion

#region 显示在表格

RowS = dTable.Rows.Count - 2;//计划表的前两行为表头,表头的第一行和第二行合并,所以表头也被加载到dataTable中,所以得减去前两行,需要的数据从第三行开始

int i = 0, j = 2;

for (i = dataGridView2.Rows.Count; i < RowS; i++)

{

if (dTable.Rows[j][2].ToString() != "")

{

#region excel导入到表格

dataGridView2.Rows.Add();

dataGridView2.Rows[i].Cells[0].Value = dTable.Rows[j][0].ToString();

dataGridView2.Rows[i].Cells[1].Value = dTable.Rows[j][1].ToString();

dataGridView2.Rows[i].Cells[2].Value = dTable.Rows[j][2].ToString();

dataGridView2.Rows[i].Cells[3].Value = dTable.Rows[j][3].ToString();

dataGridView2.Rows[i].Cells[4].Value = dTable.Rows[j][4].ToString();

dataGridView2.Rows[i].Cells[5].Value = dTable.Rows[j][5].ToString();

dataGridView2.Rows[i].Cells[6].Value = dTable.Rows[j][6].ToString();

dataGridView2.Rows[i].Cells[7].Value = dTable.Rows[j][7].ToString();

dataGridView2.Rows[i].Cells[8].Value = dTable.Rows[j][8].ToString();

dataGridView2.Rows[i].Cells[9].Value = dTable.Rows[j][9].ToString();

dataGridView2.Rows[i].Cells[10].Value = dTable.Rows[j][10].ToString();

dataGridView2.Rows[i].Cells[11].Value = dTable.Rows[j][11].ToString();

dataGridView2.Rows[i].Cells[12].Value = dTable.Rows[j][12].ToString();

dataGridView2.Rows[i].Cells[13].Value = dTable.Rows[j][13].ToString();

dataGridView2.Rows[i].Cells[14].Value = dTable.Rows[j][14].ToString();

dataGridView2.Rows[i].Cells[15].Value = dTable.Rows[j][15].ToString();

dataGridView2.Rows[i].Cells[16].Value = dTable.Rows[j][16].ToString();

dataGridView2.Rows[i].Cells[17].Value = dTable.Rows[j][17].ToString();

dataGridView2.Rows[i].Cells[18].Value = dTable.Rows[j][18].ToString();

dataGridView2.Rows[i].Cells[19].Value = dTable.Rows[j][19].ToString();

dataGridView2.Rows[i].Cells[20].Value = dTable.Rows[j][20].ToString();

dataGridView2.Rows[i].Cells[21].Value = dTable.Rows[j][21].ToString();

dataGridView2.Rows[i].Cells[22].Value = dTable.Rows[j][22].ToString();

dataGridView2.Rows[i].Cells[23].Value = dTable.Rows[j][23].ToString();

dataGridView2.Rows[i].Cells[24].Value = dTable.Rows[j][24].ToString();

dataGridView2.Rows[i].Cells[25].Value = dTable.Rows[j][25].ToString();

dataGridView2.Rows[i].Cells[26].Value = dTable.Rows[j][26].ToString();

dataGridView2.Rows[i].Cells[27].Value = dTable.Rows[j][27].ToString();

dataGridView2.Rows[i].Cells[28].Value = dTable.Rows[j][28].ToString();

dataGridView2.Rows[i].Cells[29].Value = dTable.Rows[j][29].ToString();

dataGridView2.Rows[i].Cells[30].Value = dTable.Rows[j][30].ToString();

dataGridView2.Rows[i].Cells[31].Value = dTable.Rows[j][31].ToString();

dataGridView2.Rows[i].Cells[32].Value = dTable.Rows[j][32].ToString();

dataGridView2.Rows[i].Cells[33].Value = dTable.Rows[j][33].ToString();

dataGridView2.Rows[i].Cells[34].Value = dTable.Rows[j][34].ToString();

dataGridView2.Rows[i].Cells[35].Value = dTable.Rows[j][35].ToString();

dataGridView2.Rows[i].Cells[36].Value = dTable.Rows[j][36].ToString();

dataGridView2.Rows[i].Cells[37].Value = dTable.Rows[j][37].ToString();

dataGridView2.Rows[i].Cells[38].Value = dTable.Rows[j][38].ToString();

dataGridView2.Rows[i].Cells[39].Value = dTable.Rows[j][39].ToString();

dataGridView2.Rows[i].Cells[40].Value = dTable.Rows[j][40].ToString();

dataGridView2.Rows[i].Cells[41].Value = dTable.Rows[j][41].ToString();

dataGridView2.Rows[i].Cells[42].Value = dTable.Rows[j][42].ToString();

dataGridView2.Rows[i].Cells[43].Value = dTable.Rows[j][43].ToString();

dataGridView2.Rows[i].Cells[44].Value = dTable.Rows[j][44].ToString();

dataGridView2.Rows[i].Cells[45].Value = dTable.Rows[j][45].ToString();

dataGridView2.Rows[i].Cells[46].Value = dTable.Rows[j][46].ToString();

dataGridView2.Rows[i].Cells[47].Value = dTable.Rows[j][47].ToString();

dataGridView2.Rows[i].Cells[48].Value = dTable.Rows[j][48].ToString();

dataGridView2.Rows[i].Cells[49].Value = dTable.Rows[j][49].ToString();

dataGridView2.Rows[i].Cells[50].Value = dTable.Rows[j][50].ToString();

dataGridView2.Rows[i].Cells[51].Value = dTable.Rows[j][51].ToString();

dataGridView2.Rows[i].Cells[52].Value = dTable.Rows[j][52].ToString();

dataGridView2.Rows[i].Cells[53].Value = dTable.Rows[j][53].ToString();

dataGridView2.Rows[i].Cells[54].Value = dTable.Rows[j][54].ToString();

dataGridView2.Rows[i].Cells[55].Value = dTable.Rows[j][55].ToString();

dataGridView2.Rows[i].Cells[56].Value = dTable.Rows[j][56].ToString();

dataGridView2.Rows[i].Cells[57].Value = dTable.Rows[j][57].ToString();

dataGridView2.Rows[i].Cells[58].Value = dTable.Rows[j][58].ToString();

dataGridView2.Rows[i].Cells[59].Value = dTable.Rows[j][59].ToString();

dataGridView2.Rows[i].Cells[60].Value = dTable.Rows[j][60].ToString();

dataGridView2.Rows[i].Cells[61].Value = dTable.Rows[j][61].ToString();

dataGridView2.Rows[i].Cells[62].Value = dTable.Rows[j][62].ToString();

dataGridView2.Rows[i].Cells[63].Value = dTable.Rows[j][63].ToString();

dataGridView2.Rows[i].Cells[64].Value = dTable.Rows[j][64].ToString();

dataGridView2.Rows[i].Cells[65].Value = dTable.Rows[j][65].ToString();

dataGridView2.Rows[i].Cells[66].Value = dTable.Rows[j][66].ToString();

dataGridView2.Rows[i].Cells[67].Value = dTable.Rows[j][67].ToString();

dataGridView2.Rows[i].Cells[68].Value = dTable.Rows[j][68].ToString();

dataGridView2.Rows[i].Cells[69].Value = dTable.Rows[j][69].ToString();

dataGridView2.Rows[i].Cells[70].Value = dTable.Rows[j][70].ToString();

dataGridView2.Rows[i].Cells[71].Value = dTable.Rows[j][71].ToString();

dataGridView2.Rows[i].Cells[72].Value = dTable.Rows[j][72].ToString();

dataGridView2.Rows[i].Cells[73].Value = dTable.Rows[j][73].ToString();

dataGridView2.Rows[i].Cells[74].Value = dTable.Rows[j][74].ToString();

dataGridView2.Rows[i].Cells[75].Value = dTable.Rows[j][75].ToString();

dataGridView2.Rows[i].Cells[76].Value = dTable.Rows[j][76].ToString();

dataGridView2.Rows[i].Cells[77].Value = dTable.Rows[j][77].ToString();

dataGridView2.Rows[i].Cells[78].Value = dTable.Rows[j][78].ToString();

dataGridView2.Rows[i].Cells[79].Value = dTable.Rows[j][79].ToString();

dataGridView2.Rows[i].Cells[80].Value = dTable.Rows[j][80].ToString();

dataGridView2.Rows[i].Cells[81].Value = dTable.Rows[j][81].ToString();

dataGridView2.Rows[i].Cells[82].Value = dTable.Rows[j][82].ToString();

dataGridView2.Rows[i].Cells[83].Value = dTable.Rows[j][83].ToString();

dataGridView2.Rows[i].Cells[84].Value = dTable.Rows[j][84].ToString();

dataGridView2.Rows[i].Cells[85].Value = dTable.Rows[j][85].ToString();

j++;

#endregion

}

}

j = 0;

#endregion

}

else

{

MessageBox.Show("鉴定计划表错误");

}

dTable.Dispose();

dataAdapter.Dispose();

dbCommand.Dispose();

excelConnection.Close();

excelConnection.Dispose();

sPath = "";

//j = 0;

}

else

{

MessageBox.Show("当前系统没有发现可执行的Excel文件, 如需使用Excel功能请先安装", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

#endregion

}

catch (Exception ex)

{

//return;

MessageBox.Show(ex.Message);

}

finally

{

excelConnection.Close();

excelConnection.Dispose();

}

}

/// <summary>

/// 绘制考评员计划表,这里是表格的重绘调用

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void dataGridView2_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

exGridView exG = new exGridView();

try

{

for (int i = 1; i < 7; i++)

{

colNameCollection1.Add("Column" + i.ToString());

}

for (int i = 0; i < 80; i++)

{

str1[i] = "colDraw" + i.ToString();

}

exG.MergeHeader(sender, e, str1, titleName1, colNameCollection1);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

public class exGridView : DataGridView

{

//期初行:指序号,所在单位,姓名,性别,联系方式,学员身份证号

//主子列:指题目行

//次子列:指考核时间,考评员1-5,工位,考核地点

#region 合并列时使用到的位置和大小属性

int cTop = 0;//被合并表头区域的顶部坐标

int cLeft = 0;//被合并表头区域的左边坐标

/// <summary>

/// 被合并表头区域的宽

/// </summary>

int cWidth = 0;

int cHeight = 0;//被合并表头区域的高

int cTop1 = 0, cLeft1 = 0, cWidth1 = 0, cHeight1 = 0;

static bool Flag = true;

#endregion

/// <summary>

/// 判断是否已经将datagridview的表头变高了,只增高一次。

/// </summary>

public static bool isEnLarged = false;

/// <summary>

/// 合并表头,用在dataGridView的CellPainting事件中。

/// </summary>

/// <param name="sender">需要重绘的dataGridview</param>

/// <param name="e">CellPainting中的参数</param>

///<param name="colName">列的集合(列必须是连续的,第一列放在最前面)</param>

/// <param name="headerText">列合并后显示的文本</param>

/// 画线遵循由上到下开始画,先获取上面点的横坐标,后是纵坐标,在获取下面点的横坐标,后是纵坐标

public void MergeHeader(object sender, DataGridViewCellPaintingEventArgs e, string[] colNameCollection, string[] headerText,List<string> colNameCollection1)

{

try

{

if (e.RowIndex == -1)//代表rowindex不存在,因为没有自动添加行,所以e.RowIndex始终为-1

{

DataGridView dataGridView1 = sender as DataGridView;

string colName = dataGridView1.Columns[e.ColumnIndex].Name; //e.ColumnIndex = 0代表datagridview的第0列,列名为DateSum

if (!isEnLarged) //isEnLarged = false,则!isEnLarged == true

{

//0.扩展表头高度为当前的2倍

dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;

dataGridView1.ColumnHeadersHeight = e.CellBounds.Height * 2; //当前一个表头的矩形高度是20,扩展为高度为40,也就是2倍,用来放两行,主行和分行

isEnLarged = true;

}

if (colNameCollection.Contains(colName)) //必须先将需要合并的表头添加进来,然后加载需要合并的表头和这些表头的个数添加到List中,通过Contain()方法判断是否在List当中,在的话进行重绘工作

{

#region 主次列的重绘

#region 重绘主子列,确定主子列的大小范围

//1.计算colLen个列的区域

if (Array.IndexOf(colNameCollection, colName) == 0) //搜索指定的对象,并返回整个 System.Collections.Generic.List<T> 中第一个匹配项的从零开始的索引。也就是说List也是从Index索引为0开始

{

cTop = e.CellBounds.Top; //0

cLeft = e.CellBounds.Left;//300

cWidth = e.CellBounds.Width;//第一个和需要合并的表头宽度,为50,也就是colDraw0的宽度为50,此后循环 0

cHeight = e.CellBounds.Height / 2;

int i = 0;

for (i = 1; i < 8; i++) // 1 2 3 4

{

cWidth += dataGridView1.Columns[colNameCollection[i]].Width; //cWith逐渐增加宽度

}

}

if (Array.IndexOf(colNameCollection, colName) == 8) //搜索指定的对象,并返回整个 System.Collections.Generic.List<T> 中第一个匹配项的从零开始的索引。也就是说List也是从Index索引为0开始

{

cTop = e.CellBounds.Top; //0

cLeft = e.CellBounds.Left;//300

cWidth = e.CellBounds.Width;//第一个和需要合并的表头宽度,为50,也就是colDraw0的宽度为50,此后循环 4

cHeight = e.CellBounds.Height / 2;

int i = 0;

for (i = 9; i < 16; i++)// 5 6 7

{

cWidth += dataGridView1.Columns[colNameCollection[i]].Width; //cWith逐渐增加宽度

}

}

if (Array.IndexOf(colNameCollection, colName) == 16) //搜索指定的对象,并返回整个 System.Collections.Generic.List<T> 中第一个匹配项的从零开始的索引。也就是说List也是从Index索引为0开始

{

cTop = e.CellBounds.Top; //0

cLeft = e.CellBounds.Left;//300

cWidth = e.CellBounds.Width;//第一个和需要合并的表头宽度,为50,也就是colDraw0的宽度为50,此后循环 8

cHeight = e.CellBounds.Height / 2;

int i = 0;

for (i = 17; i < 24; i++)

{

cWidth += dataGridView1.Columns[colNameCollection[i]].Width; //cWith逐渐增加宽度

}

}

if (Array.IndexOf(colNameCollection, colName) == 24) //搜索指定的对象,并返回整个 System.Collections.Generic.List<T> 中第一个匹配项的从零开始的索引。也就是说List也是从Index索引为0开始

{

cTop = e.CellBounds.Top; //0

cLeft = e.CellBounds.Left;//300

cWidth = e.CellBounds.Width;//第一个和需要合并的表头宽度,为50,也就是colDraw0的宽度为50,此后循环 12

cHeight = e.CellBounds.Height / 2;

int i = 0;

for (i = 25; i < 32; i++)

{

cWidth += dataGridView1.Columns[colNameCollection[i]].Width; //cWith逐渐增加宽度

}

}

if (Array.IndexOf(colNameCollection, colName) == 32) //搜索指定的对象,并返回整个 System.Collections.Generic.List<T> 中第一个匹配项的从零开始的索引。也就是说List也是从Index索引为0开始

{

cTop = e.CellBounds.Top; //0

cLeft = e.CellBounds.Left;//300

cWidth = e.CellBounds.Width;//第一个和需要合并的表头宽度,为50,也就是colDraw0的宽度为50,此后循环 16

cHeight = e.CellBounds.Height / 2;

int i = 0;

for (i = 33; i < 40; i++)

{

cWidth += dataGridView1.Columns[colNameCollection[i]].Width; //cWith逐渐增加宽度

}

}

if (Array.IndexOf(colNameCollection, colName) == 40) //搜索指定的对象,并返回整个 System.Collections.Generic.List<T> 中第一个匹配项的从零开始的索引。也就是说List也是从Index索引为0开始

{

cTop = e.CellBounds.Top; //0

cLeft = e.CellBounds.Left;//300

cWidth = e.CellBounds.Width;//第一个和需要合并的表头宽度,为50,也就是colDraw0的宽度为50,此后循环 20

cHeight = e.CellBounds.Height / 2;

int i = 0;

for (i = 41; i < 48; i++)

{

cWidth += dataGridView1.Columns[colNameCollection[i]].Width; //cWith逐渐增加宽度

}

}

if (Array.IndexOf(colNameCollection, colName) == 48) //搜索指定的对象,并返回整个 System.Collections.Generic.List<T> 中第一个匹配项的从零开始的索引。也就是说List也是从Index索引为0开始

{

cTop = e.CellBounds.Top; //0

cLeft = e.CellBounds.Left;//300

cWidth = e.CellBounds.Width;//第一个和需要合并的表头宽度,为50,也就是colDraw0的宽度为50,此后循环 24

cHeight = e.CellBounds.Height / 2;

int i = 0;

for (i = 49; i < 56; i++)

{

cWidth += dataGridView1.Columns[colNameCollection[i]].Width; //cWith逐渐增加宽度

}

}

if (Array.IndexOf(colNameCollection, colName) == 56) //搜索指定的对象,并返回整个 System.Collections.Generic.List<T> 中第一个匹配项的从零开始的索引。也就是说List也是从Index索引为0开始

{

cTop = e.CellBounds.Top; //0

cLeft = e.CellBounds.Left;//300

cWidth = e.CellBounds.Width;//第一个和需要合并的表头宽度,为50,也就是colDraw0的宽度为50,此后循环 28

cHeight = e.CellBounds.Height / 2;

int i = 0;

for (i = 57; i < 64; i++)

{

cWidth += dataGridView1.Columns[colNameCollection[i]].Width; //cWith逐渐增加宽度

}

}

if (Array.IndexOf(colNameCollection, colName) == 64) //搜索指定的对象,并返回整个 System.Collections.Generic.List<T> 中第一个匹配项的从零开始的索引。也就是说List也是从Index索引为0开始

{

cTop = e.CellBounds.Top; //0

cLeft = e.CellBounds.Left;//300

cWidth = e.CellBounds.Width;//第一个和需要合并的表头宽度,为50,也就是colDraw0的宽度为50,此后循环 32

cHeight = e.CellBounds.Height / 2;

int i = 0;

for (i = 65; i < 72; i++)

{

cWidth += dataGridView1.Columns[colNameCollection[i]].Width; //cWith逐渐增加宽度

}

}

if (Array.IndexOf(colNameCollection, colName) == 72) //搜索指定的对象,并返回整个 System.Collections.Generic.List<T> 中第一个匹配项的从零开始的索引。也就是说List也是从Index索引为0开始

{

cTop = e.CellBounds.Top; //0

cLeft = e.CellBounds.Left;//300

cWidth = e.CellBounds.Width;//第一个和需要合并的表头宽度,为50,也就是colDraw0的宽度为50,此后循环 36

cHeight = e.CellBounds.Height / 2;

int i = 0;

for (i = 73; i < 80; i++)

{

cWidth += dataGridView1.Columns[colNameCollection[i]].Width; //cWith逐渐增加宽度

}

}

#endregion

#region 确定好主子列的大小范围进行重绘

Rectangle cArea = new Rectangle(cLeft, cTop, cWidth, cHeight);//开始绘制所要和并的表头的主行,给定了矩形的长和宽,还有起始顶部和左部坐标,也就是绘制表头为"0-9中奖号码分布图"的行

//2.把区域设置为背景色,没有列的分线及任何文字。 仅仅是绘制了一个矩形

using (Brush backColorBrush = new SolidBrush(System.Drawing.Color.LightSkyBlue)) // 用Brush来刷矩形的颜色e.CellStyle.BackColor

{

e.Graphics.FillRectangle(backColorBrush, cArea); //填充 System.Drawing.Rectangle 结构指定的矩形的内部。backColorBrush为矩形填充所需要的颜色,cArea为填充的矩形区域

}

//3.绘制新列头的边框

using (Pen gridPen = new Pen(dataGridView1.GridColor)) //用Pen来画矩形的边框来包裹起矩形,并用相应的颜色

{

//3.1 上部边框

e.Graphics.DrawLine(gridPen, cLeft, cTop, cLeft + cWidth, cTop);//画上部边框,从cLeft开始画,画在cTop处,画的长度为cLeft + cWith

using (Pen hilightPen = new Pen(Color.WhiteSmoke)) //画好的线不是很清楚,用高光来让线条更加明显

{

//3.2 顶部高光

e.Graphics.DrawLine(hilightPen, cLeft, cTop + 1, cLeft + cWidth, cTop + 1);

//3.3 左部反光线

//e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3, cLeft, cTop + cHeight - 2); // 302 4 302 9

}

//3.4 下部边框

e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1, cLeft + cWidth, cTop + cHeight - 1);

//3.5 右部边框

e.Graphics.DrawLine(gridPen, cLeft + cWidth - 1, cTop, cLeft + cWidth - 1, cTop + cHeight);//(cTop+cHeight)/2); 右部边框,先确定右部边框一条垂直线的上下两点坐标,然后从上往下画cTop + cHeight个长度

//上面点的横坐标 cLeft + cWidth - 1

//上面点的纵坐标 cTop

//下面点的横坐标 cLeft + cWidth - 1

//下面点的纵坐标 cTop + cHeight

//减去1是以防止画出区域

}

#endregion

#region 合并好表头,进行主子列表头单元格的文本显示

if (Array.IndexOf(colNameCollection, colName) == 0)

{//不是第一列则不写文字。

int wHeadStr = (int)(headerText[0].Length * e.CellStyle.Font.SizeInPoints);

int wHeadCell = cWidth;

int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(headerText[0], e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));

}

}

if (Array.IndexOf(colNameCollection, colName) == 8)

{//不是第一列则不写文字。

int wHeadStr = (int)(headerText[1].Length * e.CellStyle.Font.SizeInPoints);

int wHeadCell = cWidth;

int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(headerText[1], e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));

}

}

if (Array.IndexOf(colNameCollection, colName) == 16)

{//不是第一列则不写文字。

int wHeadStr = (int)(headerText[2].Length * e.CellStyle.Font.SizeInPoints);

int wHeadCell = cWidth;

int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(headerText[2], e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));

}

}

if (Array.IndexOf(colNameCollection, colName) == 24)

{//不是第一列则不写文字。

int wHeadStr = (int)(headerText[3].Length * e.CellStyle.Font.SizeInPoints);

int wHeadCell = cWidth;

int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(headerText[3], e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));

}

}

if (Array.IndexOf(colNameCollection, colName) == 32)

{//不是第一列则不写文字。

int wHeadStr = (int)(headerText[4].Length * e.CellStyle.Font.SizeInPoints);

int wHeadCell = cWidth;

int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(headerText[4], e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));

}

}

if (Array.IndexOf(colNameCollection, colName) == 40)

{//不是第一列则不写文字。

int wHeadStr = (int)(headerText[5].Length * e.CellStyle.Font.SizeInPoints);

int wHeadCell = cWidth;

int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(headerText[5], e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));

}

}

if (Array.IndexOf(colNameCollection, colName) == 48)

{//不是第一列则不写文字。

int wHeadStr = (int)(headerText[6].Length * e.CellStyle.Font.SizeInPoints);

int wHeadCell = cWidth;

int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(headerText[6], e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));

}

}

if (Array.IndexOf(colNameCollection, colName) == 56)

{//不是第一列则不写文字。

int wHeadStr = (int)(headerText[7].Length * e.CellStyle.Font.SizeInPoints);

int wHeadCell = cWidth;

int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(headerText[7], e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));

}

}

if (Array.IndexOf(colNameCollection, colName) == 64)

{//不是第一列则不写文字。

int wHeadStr = (int)(headerText[8].Length * e.CellStyle.Font.SizeInPoints);

int wHeadCell = cWidth;

int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(headerText[8], e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));

}

}

if (Array.IndexOf(colNameCollection, colName) == 72)

{//不是第一列则不写文字。

int wHeadStr = (int)(headerText[9].Length * e.CellStyle.Font.SizeInPoints);

int wHeadCell = cWidth;

int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(headerText[9], e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));

}

}

#endregion

#region 重绘次子列,方法和主子列相同

int FatherColHeight = e.CellBounds.Height / 2;//上面一行的高度

using (Brush backColorBrush = new SolidBrush(System.Drawing.Color.LightSkyBlue))

{// 矩形左上角的 x 坐标。 矩形左上角的 y 坐标。 矩形的宽度。 矩形的高度

e.Graphics.FillRectangle(backColorBrush, new Rectangle(e.CellBounds.X, e.CellBounds.Y + FatherColHeight, e.CellBounds.Width - 1, e.CellBounds.Height / 2 - 1));

}

//5.1绘制子列的边框

using (Pen gridPen = new Pen(dataGridView1.GridColor))

{

using (Pen hilightPen = new Pen(Color.WhiteSmoke))

{

//5.2 左部反光线

//e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3 + FatherColHeight, cLeft, cTop + cHeight - 2 + FatherColHeight);

}

//5.3 下部边框

e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1 + FatherColHeight, cLeft + cWidth, cTop + cHeight - 1 + FatherColHeight);

//5.4 右部边框 为什么是30,而不是20

e.Graphics.DrawLine(gridPen, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Top + FatherColHeight, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Top + e.CellBounds.Height + FatherColHeight);//(cTop+cHeight)/2);

}

//5.5 写子列的文本

int wStr = (int)(dataGridView1.Columns[e.ColumnIndex].HeaderText.Length * e.CellStyle.Font.SizeInPoints);

int wCell = e.CellBounds.Width;

int pLeft = (wCell - wStr) / 2;//相对CELL左边框的左坐标

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(dataGridView1.Columns[e.ColumnIndex].HeaderText, e.CellStyle.Font, foreBrush, new PointF(e.CellBounds.X + pLeft - 15, cTop + 3 + FatherColHeight));

}

e.Handled = true;

#endregion

#endregion

}

else

{

#region 期初行配置

//1.计算colLen个列的区域

cTop1 = e.CellBounds.Top; //被合并表头区域的顶部坐标

cLeft1 = e.CellBounds.Left;//被合并表头区域的左部坐标

cWidth1 = e.CellBounds.Width; //被合并表头区域的宽

cHeight1 = e.CellBounds.Height * 2;//被合并表头区域的高

Rectangle cArea = new Rectangle(cLeft1, cTop1, cWidth1, cHeight1);

using (Brush backColorBrush = new SolidBrush(System.Drawing.Color.LightSkyBlue)) // 用Brush来刷矩形的颜色e.CellStyle.BackColor

{

e.Graphics.FillRectangle(backColorBrush, cArea);

}

//3.绘制新列头的边框

using (Pen gridPen = new Pen(dataGridView1.GridColor))

{

//3.1左部边框

if (Flag == true)

{

e.Graphics.DrawLine(gridPen, cLeft1, cTop1, cLeft1, cTop1 + cHeight1);

Flag = false;

}

//3.2 上部边框

e.Graphics.DrawLine(gridPen, cLeft1, cTop1, cLeft1 + cWidth1, cTop1);

using (Pen hilightPen = new Pen(Color.WhiteSmoke))

{

//3.3 顶部高光

e.Graphics.DrawLine(hilightPen, cLeft1, cTop1 + 1, cLeft1 + cWidth1, cTop1 + 1);//对

}

//3.4 下部边框

e.Graphics.DrawLine(gridPen, cLeft1, cTop1 + cHeight1 / 2 - 1, cLeft1 + cWidth1, cTop1 + cHeight1 / 2 - 1);//下边框上面

//3.5 右部边框线

e.Graphics.DrawLine(gridPen, cLeft1 + cWidth1 - 1, cTop1, cLeft1 + cWidth1 - 1, cTop1 + cHeight1 / 2 - 1);

}

//4.写文本

string str1 = dataGridView1.Columns[e.ColumnIndex].HeaderText;

switch (str1)

{

case "序号":

{

int wHeadStr = (int)(str1.Length * e.CellStyle.Font.SizeInPoints) / 2;

int wHeadCell = cWidth1 / 2;

int pHeadLeft = (wHeadCell - wHeadStr);

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(str1, e.CellStyle.Font, foreBrush, new PointF(cLeft1 + pHeadLeft - 5, cTop1 + 14));

}

break;

}

case "所在单位":

{

int wHeadStr = (int)(str1.Length * e.CellStyle.Font.SizeInPoints) / 2;

int wHeadCell = cWidth1 / 2;

int pHeadLeft = (wHeadCell - wHeadStr);

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(str1, e.CellStyle.Font, foreBrush, new PointF(cLeft1 + pHeadLeft - 18, cTop1 + 14));

}

break;

}

case "姓名":

{

int wHeadStr = (int)(str1.Length * e.CellStyle.Font.SizeInPoints) / 2;

int wHeadCell = cWidth1 / 2;

int pHeadLeft = (wHeadCell - wHeadStr);

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(str1, e.CellStyle.Font, foreBrush, new PointF(cLeft1 + pHeadLeft - 5, cTop1 + 14));

}

break;

}

case "性别":

{

int wHeadStr = (int)(str1.Length * e.CellStyle.Font.SizeInPoints) / 2;

int wHeadCell = cWidth1 / 2;

int pHeadLeft = (wHeadCell - wHeadStr);

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(str1, e.CellStyle.Font, foreBrush, new PointF(cLeft1 + pHeadLeft - 5, cTop1 + 14));

}

break;

}

case "联系方式":

{

int wHeadStr = (int)(str1.Length * e.CellStyle.Font.SizeInPoints) / 2;

int wHeadCell = cWidth1 / 2;

int pHeadLeft = (wHeadCell - wHeadStr);

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(str1, e.CellStyle.Font, foreBrush, new PointF(cLeft1 + pHeadLeft - 5, cTop1 + 14));

}

break;

}

case "学员身份证号":

{

int wHeadStr = (int)(str1.Length * e.CellStyle.Font.SizeInPoints) / 2;

int wHeadCell = cWidth1 / 2;

int pHeadLeft = (wHeadCell - wHeadStr);

using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))

{

e.Graphics.DrawString(str1, e.CellStyle.Font, foreBrush, new PointF(cLeft1 + pHeadLeft - 5, cTop1 + 14));

}

break;

}

default:

{

break;

}

}

e.Handled = true;

#endregion

}

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

return;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: