|
|
@@ -43,110 +43,118 @@ namespace HKCardManager.UserPages |
|
|
|
|
|
|
|
private void ExportDataToExcel(DataGridView TableName, string FileName) |
|
|
|
{ |
|
|
|
var d = TableName.DataSource as List<ReportCountDto>; |
|
|
|
SaveFileDialog saveFileDialog = new SaveFileDialog(); |
|
|
|
saveFileDialog.Title = "导出Excel文件"; //设置文件标题 |
|
|
|
saveFileDialog.Filter = "Microsoft Office Excel 工作簿(*.xls)|*.xls"; //设置文件类型 |
|
|
|
saveFileDialog.FilterIndex = 1; //设置默认文件类型显示顺序 |
|
|
|
saveFileDialog.AddExtension = true; //是否自动在文件名中添加扩展名 |
|
|
|
saveFileDialog.RestoreDirectory = true; //是否记忆上次打开的目录 |
|
|
|
saveFileDialog.FileName = FileName; //设置默认文件名 |
|
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK) |
|
|
|
try |
|
|
|
{ |
|
|
|
string localFilePath = saveFileDialog.FileName.ToString(); |
|
|
|
//数据初始化 |
|
|
|
int TotalCount; //总行数 |
|
|
|
int RowRead = 0; //已读行数 |
|
|
|
int Percent = 0; //百分比 |
|
|
|
TotalCount = TableName.Rows.Count; |
|
|
|
Stream myStream = saveFileDialog.OpenFile(); |
|
|
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); |
|
|
|
StreamWriter sw = new StreamWriter(myStream, Encoding.GetEncoding("gb2312")); |
|
|
|
string strHeader = string.Empty; |
|
|
|
var d = TableName.DataSource as List<ReportCountDto>; |
|
|
|
SaveFileDialog saveFileDialog = new SaveFileDialog(); |
|
|
|
saveFileDialog.Title = "导出Excel文件"; //设置文件标题 |
|
|
|
saveFileDialog.Filter = "Microsoft Office Excel 工作簿(*.xls)|*.xls"; //设置文件类型 |
|
|
|
saveFileDialog.FilterIndex = 1; //设置默认文件类型显示顺序 |
|
|
|
saveFileDialog.AddExtension = true; //是否自动在文件名中添加扩展名 |
|
|
|
saveFileDialog.RestoreDirectory = true; //是否记忆上次打开的目录 |
|
|
|
saveFileDialog.FileName = FileName; //设置默认文件名 |
|
|
|
if (saveFileDialog.ShowDialog() == DialogResult.OK) |
|
|
|
{ |
|
|
|
string localFilePath = saveFileDialog.FileName.ToString(); |
|
|
|
//数据初始化 |
|
|
|
int TotalCount; //总行数 |
|
|
|
int RowRead = 0; //已读行数 |
|
|
|
int Percent = 0; //百分比 |
|
|
|
TotalCount = TableName.Rows.Count; |
|
|
|
Stream myStream = saveFileDialog.OpenFile(); |
|
|
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); |
|
|
|
StreamWriter sw = new StreamWriter(myStream, Encoding.GetEncoding("gb2312")); |
|
|
|
string strHeader = string.Empty; |
|
|
|
|
|
|
|
Stopwatch timer = new Stopwatch(); |
|
|
|
timer.Start(); |
|
|
|
Stopwatch timer = new Stopwatch(); |
|
|
|
timer.Start(); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
//写入标题 |
|
|
|
for (int i = 0; i < TableName.Columns.Count; i++) |
|
|
|
try |
|
|
|
{ |
|
|
|
if (TableName.Columns[i].Visible) |
|
|
|
//写入标题 |
|
|
|
for (int i = 0; i < TableName.Columns.Count; i++) |
|
|
|
{ |
|
|
|
if (i > 0 && !string.IsNullOrEmpty(strHeader)) strHeader += "\t"; |
|
|
|
strHeader += TableName.Columns[i].HeaderText.ToString(); |
|
|
|
if (TableName.Columns[i].Visible) |
|
|
|
{ |
|
|
|
if (i > 0 && !string.IsNullOrEmpty(strHeader)) strHeader += "\t"; |
|
|
|
strHeader += TableName.Columns[i].HeaderText.ToString(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
sw.WriteLine(strHeader); |
|
|
|
for (int i = 0; i < TableName.Rows.Count+1; i++) |
|
|
|
{ |
|
|
|
RowRead++; |
|
|
|
Percent = (int)(100 * RowRead / (TotalCount+1)); |
|
|
|
Application.DoEvents(); |
|
|
|
string strData = string.Empty; |
|
|
|
for (int j = 0; j < TableName.Columns.Count; j++) |
|
|
|
sw.WriteLine(strHeader); |
|
|
|
for (int i = 0; i < TableName.Rows.Count + 1; i++) |
|
|
|
{ |
|
|
|
if (i == TableName.Rows.Count) |
|
|
|
RowRead++; |
|
|
|
Percent = (int)(100 * RowRead / (TotalCount + 1)); |
|
|
|
Application.DoEvents(); |
|
|
|
string strData = string.Empty; |
|
|
|
for (int j = 0; j < TableName.Columns.Count; j++) |
|
|
|
{ |
|
|
|
if (TableName.Columns[j].Visible && TableName.Columns[j].Name == "早上消费金额") |
|
|
|
{ |
|
|
|
if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t"; |
|
|
|
strData += d.Sum(t => t.AM); |
|
|
|
} |
|
|
|
else if (TableName.Columns[j].Visible && TableName.Columns[j].Name == "中午消费金额") |
|
|
|
{ |
|
|
|
if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t"; |
|
|
|
strData += d.Sum(t => t.PM); |
|
|
|
} |
|
|
|
else if (TableName.Columns[j].Visible && TableName.Columns[j].Name == "晚上消费金额") |
|
|
|
if (i == TableName.Rows.Count) |
|
|
|
{ |
|
|
|
if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t"; |
|
|
|
strData += d.Sum(t => t.AT); |
|
|
|
} |
|
|
|
else if (TableName.Columns[j].Visible && TableName.Columns[j].Name == "金额总计") |
|
|
|
{ |
|
|
|
if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t"; |
|
|
|
strData += d.Sum(t => t.Total); |
|
|
|
} |
|
|
|
else if(TableName.Columns[j].Visible) |
|
|
|
{ |
|
|
|
if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t"; |
|
|
|
strData += "-"; |
|
|
|
if (TableName.Columns[j].Visible && TableName.Columns[j].Name == "早上消费金额") |
|
|
|
{ |
|
|
|
if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t"; |
|
|
|
strData += d.Sum(t => t.AM); |
|
|
|
} |
|
|
|
else if (TableName.Columns[j].Visible && TableName.Columns[j].Name == "中午消费金额") |
|
|
|
{ |
|
|
|
if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t"; |
|
|
|
strData += d.Sum(t => t.PM); |
|
|
|
} |
|
|
|
else if (TableName.Columns[j].Visible && TableName.Columns[j].Name == "晚上消费金额") |
|
|
|
{ |
|
|
|
if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t"; |
|
|
|
strData += d.Sum(t => t.AT); |
|
|
|
} |
|
|
|
else if (TableName.Columns[j].Visible && TableName.Columns[j].Name == "金额总计") |
|
|
|
{ |
|
|
|
if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t"; |
|
|
|
strData += d.Sum(t => t.Total); |
|
|
|
} |
|
|
|
else if (TableName.Columns[j].Visible) |
|
|
|
{ |
|
|
|
if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t"; |
|
|
|
strData += "-"; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
if (i <= TableName.Rows.Count-1) |
|
|
|
else |
|
|
|
{ |
|
|
|
string? res = TableName.Rows[i].Cells[j].Value?.ToString(); |
|
|
|
if (TableName.Columns[j].Visible) |
|
|
|
if (i <= TableName.Rows.Count - 1) |
|
|
|
{ |
|
|
|
if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t"; |
|
|
|
strData += string.IsNullOrEmpty(res) ? "" : res; |
|
|
|
string? res = TableName.Rows[i].Cells[j].Value?.ToString(); |
|
|
|
if (TableName.Columns[j].Visible) |
|
|
|
{ |
|
|
|
if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t"; |
|
|
|
strData += string.IsNullOrEmpty(res) ? "" : res; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
sw.WriteLine(strData); |
|
|
|
} |
|
|
|
sw.WriteLine(strData); |
|
|
|
sw.Close(); |
|
|
|
myStream.Close(); |
|
|
|
timer.Reset(); |
|
|
|
timer.Stop(); |
|
|
|
} |
|
|
|
sw.Close(); |
|
|
|
myStream.Close(); |
|
|
|
timer.Reset(); |
|
|
|
timer.Stop(); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
|
sw.Close(); |
|
|
|
myStream.Close(); |
|
|
|
timer.Stop(); |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
|
sw.Close(); |
|
|
|
myStream.Close(); |
|
|
|
timer.Stop(); |
|
|
|
} |
|
|
|
MessageLogNotify.GetInstance.Show("数据导出成功"); |
|
|
|
} |
|
|
|
MessageLogNotify.GetInstance.Show("数据导出成功"); |
|
|
|
} |
|
|
|
catch (Exception) |
|
|
|
{ |
|
|
|
MessageBox.Show("文件已被打开,请先关闭后在导出", "提示"); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
List<ReportCountDto> reportCountDtos = new List<ReportCountDto>(); |
|
|
|
//查询 |
|
|
@@ -182,7 +190,7 @@ namespace HKCardManager.UserPages |
|
|
|
{ |
|
|
|
this.Invoke(() => |
|
|
|
{ |
|
|
|
this.button3.Enabled = true; |
|
|
|
this.button4.Enabled = true; |
|
|
|
if (type == 1) |
|
|
|
{ |
|
|
|
dataGridView1.Columns[0].Visible = true; |
|
|
|