最近做了一个小功能,就是导出Excel文件时,设置进度条来显示导出进度。
导出Excel文件我使用的NPOI实现的,进度条我是用ProgressBar做的。
我设置进度条的基本思路是:
把list数组中的数据包的总条数作为进度条的最大值,进度条的最小值设置为0,循环list数组,每循环一条,就把listrowcount的值加一(对listrowcount的值进行累加),最后把listrowcount的值给进度条的当前值。
自己重新做了一个小例子,例子比较简单,可能还有许多不足之处。
效果图如下:
//导出按钮
private void ExportBtn_Click(object sender, EventArgs e)
{
Export();
}
public void Export()
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = true;
fileDialog.Title = "请选择文件";
fileDialog.Filter = "所有文件(*.*)|*.*";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string file = fileDialog.FileName;
string openfilename = System.IO.Path.GetFileNameWithoutExtension(file);//获取打开的文件名
#region 将选择的文件内容读取到buffer数组中,截取后存到List中
//数据包内容是字节数组
FileStream fStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[fStream.Length];
fStream.Read(buffer, 0, buffer.Length);
fStream.Dispose();
List list = new List();
int num = 0;
for (int i = 0; i < buffer.Length; i++)
{
Byte[] bytes = buffer.Skip(num).Take(10).ToArray();
list.Add(bytes);
num += 10;
i = num;
}
#endregion
#region 打开文件保存窗口
//打开保存文件窗口
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = file;//设置默认文件名
#endregion
if (saveDialog.ShowDialog() == DialogResult.OK)
{
saveFileName = saveDialog.FileName;//设置文件保存名
//设置进度条
CheckForIllegalCrossThreadCalls = false;
long totalcount = list.Count;//list中储存的数据包的条数,将条数设置为进度条的最大值
toolStripProgressBar1.Visible = true;//设置进度条可见
toolStripProgressBar1.Value = 0;//设置进度条当前值为0
toolStripProgressBar1.Minimum = 0;//设置进度条最小值为0
toolStripProgressBar1.Maximum = (int)totalcount;//设置进度条的最大值为总条数
long listrowcount = 0;
try
{
//导出文件方法
bool exportresult = ExportExcelFile(list, saveFileName, listrowcount);
if (exportresult == true)
{
MessageBox.Show("导出成功!");
}
else
{
MessageBox.Show("导出失败!");
toolStripProgressBar1.Value = 0;//设置进度条当前位置为0
}
}
catch (Exception ex)
{
MessageBox.Show("异常信息:" + ex.Message);
};
}
}
}
///
/// 文件导出
///
/// 存储数据包数据
/// 保存的文件名
/// list中截取数据包数据的条数
///
public bool ExportExcelFile(List list, string saveFileName, long listrowcount)
{
HSSFWorkbook workbook = new HSSFWorkbook();//创建工作簿
ISheet sheet = workbook.CreateSheet("Sheet1");//创建sheet页
for (int m = 0; m < list.Count; m++)
{
try
{
//格式设置
IRow RowCenter = sheet.CreateRow( m + 1);
ICellStyle styleHeader = workbook.CreateCellStyle();
styleHeader.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
IFont font = workbook.CreateFont();
font.Boldweight = short.MaxValue;
font.FontName = "宋体";
font.FontHeightInPoints = (short)17;
styleHeader.SetFont(font);
IRow RowHeader2 = sheet.CreateRow(0);
//列名
RowHeader2.CreateCell(0).SetCellValue("aaa");
RowHeader2.CreateCell(1).SetCellValue("bbb");
RowHeader2.CreateCell(2).SetCellValue("ccc");
RowHeader2.CreateCell(3).SetCellValue("ddd");
RowHeader2.CreateCell(4).SetCellValue("eee");
RowHeader2.CreateCell(5).SetCellValue("fff");
RowHeader2.CreateCell(6).SetCellValue("ggg");
RowHeader2.CreateCell(7).SetCellValue("hhh");
RowHeader2.CreateCell(8).SetCellValue("iii");
RowHeader2.CreateCell(9).SetCellValue("jjj");
string[] data = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
for (int i = 0; i < data.Length; i++)
{
ICell cell = RowCenter.CreateCell(i);
cell.SetCellValue(data[i]);
}
listrowcount++;
}
catch(Exception ex)
{
MessageBox.Show("异常信息:" + ex.Message);
return false;
}
}
toolStripProgressBar1.Value = (int)listrowcount;
FileStream filename = new FileStream(saveFileName, FileMode.Create);
workbook.Write(filename);
filename.Close();
filename.Dispose();
return true;
}