标签:计算 iss 字段 gdi+ gre column book type ESS
public bool DataGridviewShowToExcel(DataGridView dgv, bool isShowExcle) //实现导出 EXcel 和 滚动条功能
{
if (dgv.Rows.Count == 0)
return false;
//建立Excel对象
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
progressBar.Maximum = dgv.RowCount;
progressBar.Step = dgv.RowCount /40;
progressBar.Value = 0;
//labelvalue.Visible = true; //先显示Label标签才能置于 进度条之上
//labelvalue.Parent = progressBar;
//labelvalue.Location = new Point(progressBar.Width/2-labelvalue.Width/2,progressBar.Height/2-labelvalue.Height/2); //计算在父容器 的 居中位置
var point = new Point(progressBar.Width / 2 - labelvalue.Width / 2, progressBar.Height / 2 - labelvalue.Height / 2);
progressBar.Visible = true;
Graphics g = progressBar.CreateGraphics(); //在滚动条上 创建 GDI+绘图对象
//生成字段名称
for (int i = 0; i < dgv.ColumnCount; i++)
{
excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < dgv.RowCount - 1; i++)
{
for (int j = 0; j < dgv.ColumnCount; j++)
{
//if (dgv[j, i].ValueType == typeof(DateTime))
//{
// excel.Cells[i + 2, j + 1].NumberFormatLocal = "yyyy-MM-dd HH:mm:ss";
//}
if (dgv[j, i].ValueType == typeof(DateTime))
{
excel.Cells[i + 2, j + 1] = "‘" + dgv[j, i].Value.ToString(); //EXcel 文本方式显示
}
else
{
excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();
}
}
if((i+1)% progressBar.Step == 0) //判断递增进度
{
progressBar.PerformStep();
labelvalue.Text = String.Format("加载中:{0:0%}", (float)progressBar.Value/ dgv.RowCount); //先转换浮点数运算,再把浮点数转百分比
if (progressBar.Maximum / progressBar.Value < 2)
{
g.DrawString(labelvalue.Text, new Font("Arial", 10), Brushes.Yellow, point); //在居中位置绘制 进度数值
}
else
{
g.DrawString(labelvalue.Text, new Font("Arial", 10), Brushes.Blue, point); //在居中位置绘制 进度数值 变色适应背景
}
}
}
labelvalue.Visible = false;
progressBar.Visible = false;
excel.Visible = true; //导出完成弹出表格 以防出错
return true;
}
标签:计算 iss 字段 gdi+ gre column book type ESS
原文地址:https://www.cnblogs.com/Bing-yang/p/12980823.html