string saveFileName = "";
bool fileSaved = false;
string DatePartSrt = "";
string DatePart = DateTime.Now.ToString().Replace('-', ' ').Replace(':', ' ');
string[] DatePartArray = DatePart.Split(' ');
foreach (string str in DatePartArray)
{
DatePartSrt += str.ToString();
}
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "职工定额工资分配结算表Excel文件|*.xls";
saveDialog.FileName = "职工定额工资分配结算表(" + DatePartSrt + ")";
if (saveDialog.ShowDialog() != DialogResult.OK)
return;
saveFileName = saveDialog.FileName;
Excel.Application xlApp = new Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的电脑未安装Excel!");
xlApp.Quit();
GC.Collect();
return;
}
xlApp.Application.DisplayAlerts = false;//关闭提示
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
int TaskID = -1;//记录任务ID
RowIndex = 1;//初始化行索引
int BorderIndex;//画表格
for (int r = 0; r < ExportList.Count; r++)
{
//开始新任务的输出
if (TaskID != ExportList[r].id)
{
if (RowIndex != 1)
{
List<KeporModel.CompenstationAdjustModel> SetSum = new List<KeporModel.CompenstationAdjustModel>();
SetSum.AddRange(ExportList.FindAll(delegate(KeporModel.CompenstationAdjustModel model) { return model.id == TaskID; }));
worksheet.Cells[RowIndex + 2, 3] = "合计:";
worksheet.Cells[RowIndex + 2, 4] = AdjustSun(SetSum, 2).ToString();
worksheet.Cells[RowIndex + 2, 6] = AdjustSun(SetSum, 0).ToString();
worksheet.Cells[RowIndex + 2, 7] = AdjustSun(SetSum, 1).ToString();
SetSum.Clear();
BorderIndex = RowIndex;
RowIndex += 3;
}
Excel.Range End = worksheet.get_Range("A" + RowIndex.ToString(), "H" + RowIndex.ToString());
Excel.Range Pre = worksheet.get_Range("A" + RowIndex.ToString(), "B" + RowIndex.ToString());
Pre.Merge(false);
Pre.Borders.Weight = 2;
Excel.Range rH = worksheet.get_Range("A" + RowIndex.ToString(), "H" + RowIndex.ToString());
rH.Font.Name = "宋体";
rH.Font.Size = 14;
rH.Font.ColorIndex = 0;//3是红色
rH.Merge(0);//合并单元格
rH.Font.Bold = true;//加粗
rH.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//
rH.HorizontalAlignment = XlVAlign.xlVAlignCenter;//
rH.Borders.Weight = 2;
worksheet.Cells[RowIndex, 1] = "班组 " + ExportList[r].F1 + " 职工定额工资分配结算表";
//写入标题
worksheet.get_Range("A" + (RowIndex + 1).ToString(), "H" + (RowIndex + 1).ToString()).Font.Size = 14;
worksheet.Cells[RowIndex + 1, 1] = "序号";
worksheet.Cells[RowIndex + 1, 2] = "员工编号";
worksheet.Cells[RowIndex + 1, 3] = "员工姓名";
worksheet.Cells[RowIndex + 1, 4] = "出勤";
worksheet.Cells[RowIndex + 1, 5] = "成色";
worksheet.Cells[RowIndex + 1, 6] = "计算工资";
worksheet.Cells[RowIndex + 1, 7] = "实发工资";
worksheet.Cells[RowIndex + 1, 8] = "薪资状态";
//写入数据
worksheet.Cells[RowIndex + 2, 1] = (r + 1).ToString();
worksheet.Cells[RowIndex + 2, 2] = ExportList[r].F3;
worksheet.Cells[RowIndex + 2, 3] = ExportList[r].F2;
worksheet.Cells[RowIndex + 2, 4] = ExportList[r].WorkDays;
worksheet.Cells[RowIndex + 2, 5] = ExportList[r].Quality;
worksheet.Cells[RowIndex + 2, 6] = CoreCommon.Kepor_String.getFormantedString(ExportList[r].PayoffSum.ToString(),8) + " 元";
worksheet.Cells[RowIndex + 2, 7] = CoreCommon.Kepor_String.getFormantedString(ExportList[r].OutPayoff.ToString(),8)+" 元";
worksheet.Cells[RowIndex + 2, 8] = States(ExportList[r].States);
End.Borders.Weight = 2;
}
else
{
//写入数据
//Excel.Range End = worksheet.get_Range("A" + RowIndex.ToString(), "H" + RowIndex.ToString());
Excel.Range End = worksheet.get_Range("A" + (RowIndex+3).ToString(), "H" + RowIndex.ToString());
worksheet.Cells[RowIndex + 2, 1] = (r + 1).ToString();
worksheet.Cells[RowIndex + 2, 2] = ExportList[r].F3;
worksheet.Cells[RowIndex + 2, 3] = ExportList[r].F2;
worksheet.Cells[RowIndex + 2, 4] = ExportList[r].WorkDays;
worksheet.Cells[RowIndex + 2, 5] = ExportList[r].Quality;
worksheet.Cells[RowIndex + 2, 6] = CoreCommon.Kepor_String.getFormantedString(ExportList[r].PayoffSum.ToString(),8)+" 元";
worksheet.Cells[RowIndex + 2, 7] = CoreCommon.Kepor_String.getFormantedString(ExportList[r].OutPayoff.ToString(),8)+" 元";
worksheet.Cells[RowIndex + 2, 8] = States(ExportList[r].States);
End.Borders.Weight = 2;
}
RowIndex++ ;
TaskID = ExportList[r].id;
System.Windows.Forms.Application.DoEvents();
}
//
List<KeporModel.CompenstationAdjustModel> SetSum2 = new List<KeporModel.CompenstationAdjustModel>();
SetSum2.AddRange(ExportList.FindAll(delegate(KeporModel.CompenstationAdjustModel model) { return model.id== TaskID; }));
worksheet.Cells[RowIndex + 2, 3] = "合计:";
worksheet.Cells[RowIndex + 2, 4] = AdjustSun(SetSum2, 2).ToString();
worksheet.Cells[RowIndex + 2, 6] = AdjustSun(SetSum2, 0).ToString();
worksheet.Cells[RowIndex + 2, 7] = AdjustSun(SetSum2, 1).ToString();
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
fileSaved = true;
}
catch (Exception ex)
{
fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!/n" + ex.Message);
}
}
else
{
fileSaved = false;
}
xlApp.Quit();
GC.Collect();//垃圾回收
if (fileSaved && System.IO.File.Exists(saveFileName))
if (MessageBox.Show("文件导出成功,是否打开文件?", SystemTitle + "-友情提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
return;
System.Diagnostics.Process.Start(saveFileName); //打开EXCEL