以前在项目中遇到过将Excel表中的坐标信息读取出来,并用这些坐标信息在窗体上绘制圆形,大致预览出形状,在这里主要用到了NPOI及GDI+绘图,特把此分享记录下来。
项目要求:
1、根据要求EXCEL表第一列是ID号,要在每个圆里显示出来;
2、EXCEL表第三列与第四列分别是X,Y坐标,无论第一个点坐标是不是原点,绘制时都要在固定的地方开始;
3、EXCEL表的第四列是坐标显示信息,只有0和1,是0时显示一种颜色,是1时显示另外一种颜色。
部分代码如下:
1、读取EXCEL表放到DataGridView中,首先引用NPOI
string path; | |
OpenFileDialog openFileDialog = new OpenFileDialog(); | |
openFileDialog.InitialDirectory = @"./"; | |
openFileDialog.Filter = "EXCEL2007|*.xlsx";//EXCEL2003|*.xls| | |
openFileDialog.RestoreDirectory = true; | |
openFileDialog.FilterIndex = 1; | |
if (openFileDialog.ShowDialog() == DialogResult.OK) | |
{ | |
dt.Reset(); | |
dt = new DataTable(); | |
path = openFileDialog.FileName; | |
textBox1.Text = path; | |
using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read)) | |
{ | |
IWorkbook wk = new XSSFWorkbook(fsRead); | |
//new XSSFWorkbook | |
ISheet sheet = wk.GetSheetAt(0); | |
IRow currentRow = sheet.GetRow(0); | |
//最后一个方格的编号 即总的列数 | |
int cellCount = currentRow.LastCellNum; | |
for (int i = 0; i < cellCount; i++) | |
{ | |
//DataColumn dc = new DataColumn(); | |
dt.Columns.Add(currentRow.GetCell(i).ToString());//增加列 | |
} | |
for (int j = 0; j <= sheet.LastRowNum - 1; j++) | |
{ | |
//DataRow dr = dt.NewRow(); | |
dt.Rows.Add();//增加行 | |
IRow currentRow1 = sheet.GetRow(j + 1); | |
if (currentRow1 != null) | |
{ | |
for (int c = 0; c < currentRow1.LastCellNum; c++) | |
{ | |
ICell cell = currentRow1.GetCell(c); | |
if (cell == null || cell.CellType == CellType.Blank) | |
{ | |
//向dt中插入一个DBNull.Value | |
dt.Rows[j][c] = DBNull.Value; | |
} | |
else | |
{ | |
//如果当前单元格不为空,则根据单元格的类型获取数据 | |
switch (cell.CellType) | |
{ | |
//只有当单元格的数据类型是数字类型的时候使用cell.NumericCellValue来获取值。其余类型都使用字符串来获取.日期类型数据插入单元格后也是CellType.NUMERIC | |
case CellType.Numeric: | |
//cell.NumericCellValue; | |
dt.Rows[j][c] = cell.NumericCellValue; | |
break; | |
default: | |
//cell.ToString(); | |
dt.Rows[j][c] = cell.ToString(); | |
break; | |
} | |
} | |
// this.dataGridView1.Rows[index].Cells[0].Value = "1"; | |
} | |
} | |
} | |
dataGridView1.DataSource = dt; | |
} | |
} | |
else | |
return; |
2、图形绘制
private void PaintCircle(float x, float y, int i, int j, DataTable dtemp) | |
{ | |
float dt_01x = Convert.ToSingle(dt.Rows[0][1]); | |
float dt_01y = Convert.ToSingle(dt.Rows[0][2]); | |
Graphics g = this.CreateGraphics(); | |
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; | |
Brush bushO = new SolidBrush(Color.Orange);//填充的颜色 | |
Brush bushG = new SolidBrush(Color.Green);//填充的颜色 | |
Brush bush1 = new SolidBrush(Color.Red);//填充的颜色 | |
if (j == 0) | |
{ | |
g.FillEllipse(bushG, 400 - 1.2f*x + dt_01x, 50 + 1.2f*y - dt_01y, 18, 18);//画填充椭圆的方法,x坐标、y坐标、宽、高,如果是100,则半径为50 | |
} | |
else | |
{ | |
g.FillEllipse(bushO, 400 - 1.2f * x + dt_01x, 50 + 1.2f * y - dt_01y, 18, 18);//画填充椭圆的方法,x坐标、y坐标、宽、高,如果是100,则半径为50 | |
} | |
g.DrawString((i + 1).ToString(), new Font("黑体", 9f, FontStyle.Bold), bush1, 400 - 1.2f * x + dt_01x, 50 + 1.2f * y - dt_01y); | |
g.Dispose(); | |
bushO.Dispose(); | |
bushG.Dispose(); | |
bush1.Dispose(); | |
} |
3、效果动态预览
4、完整代码较长,已存放云盘,点赞并关注@工控上位机 ,发送私信“绘制圆”即可获取,非常谢谢您的关注,一起学习进步。