using System;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
using Microsoft.Office.Interop.Excel;
using BIReport.BLL;
using BIReport.Model;
using BIReport.Tools;
namespace BIReport
{
public abstract class RunExcel
{
/// <summary>
/// count date before
/// </summary>
/// <param name="date"></param>
public static EmailContent BuildExcel(DateTime date)
{
EmailContent ec = new EmailContent();
string fileName = string.Empty;
string content = string.Empty;
DateTime currentDate = date.Day == 1 ? date.AddDays(-1) : date;
DateTime preDate = currentDate.AddMonths(-1);
int currentYear = currentDate.Year;
int currentMonth = currentDate.Month;
int currentDay = currentDate.Day;
int preYear = preDate.Year;
int preMonth = preDate.Month;
int preDay = preDate.Day;
#region getdata
Log.LogRun(string.Format("{0}->currentDoc start", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
XmlDocument currentDoc = RunXML.GetData(date);
Log.LogRun(string.Format("{0}->currentDoc end", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
DateTime previousDate = date.AddMonths(-1);
Log.LogRun(string.Format("{0}->previousDoc start", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
XmlDocument previousDoc = RunXML.GetData(new DateTime(currentYear, currentMonth, 1));
Log.LogRun(string.Format("{0}->previousDoc end", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
Log.LogRun(string.Format("{0}->GetTotalTop start", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
List<TeamOrder> tatolTop = TeamOrderBLL.GetTotalTop(10, date);
Log.LogRun(string.Format("{0}->GetTotalTop end", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
//List<TeamOrder> descTop = TeamOrderBLL.GetDescTop(10, date);
Log.LogRun(string.Format("{0}->GetProfitTop start", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
List<TeamOrder> ProfitTop = TeamOrderBLL.GetProfitTop(10, date);
Log.LogRun(string.Format("{0}->GetProfitTop end", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
Log.LogRun(string.Format("{0}->GetCostTop start", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
List<TeamOrder> costTop = TeamOrderBLL.GetCostTop(10, date);
Log.LogRun(string.Format("{0}->GetCostTop end", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
Log.LogRun(string.Format("{0}->build excel start", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
#endregion
Microsoft.Office.Interop.Excel.Application excelApp = null;
try
{
excelApp = ExcelHelper.GetExcelApp(Common.TemplatePath, Common.TempPath.Replace("$date$", date.AddDays(-1).ToString("yyyyMMdd")));
Worksheet sheet1 = excelApp.Worksheets[1];
#region title
XmlNodeList departs = currentDoc.SelectNodes("/root/item[@date='" + (date.AddDays(-1).Day < 10 ? "0" + date.AddDays(-1).Day : date.AddDays(-1).Day.ToString()) + "']/depart");
decimal tatolpre = 0;
decimal profitpre = 0;
if (departs != null && departs.Count > 0)
{
foreach (XmlNode depart in departs)
{
tatolpre += StringUtility.GetDecimalFromString(depart.Attributes["tatol"].Value, 0);
profitpre += StringUtility.GetDecimalFromString(depart.Attributes["profit"].Value, 0);
}
}
string titel = sheet1.Cells[1, 2].text;
titel = titel.Replace("$tatolpre$", (tatolpre / 10000).ToString("0.00"));
decimal mtd = 0;
decimal mtdpercent = 0;
decimal mtdprofit = 0;
decimal mtdprofitpercent = 0;
int mtdlength = date.AddDays(-1).Day;
for (int i = 0; i < mtdlength; i++)
{
XmlNodeList mtdlist = currentDoc.SelectNodes("/root/item[@date='" + ((i + 1) < 10 ? "0" + (i + 1) : (i + 1).ToString()) + "']/depart");
XmlNodeList mtdpercentlist = previousDoc.SelectNodes("/root/item[@date='" + ((i + 1) < 10 ? "0" + (i + 1) : (i + 1).ToString()) + "']/depart");
foreach (XmlNode depart in mtdlist)
{
mtd += StringUtility.GetDecimalFromString(depart.Attributes["tatol"].Value, 0);
mtdprofit += StringUtility.GetDecimalFromString(depart.Attributes["profit"].Value, 0);
}
foreach (XmlNode depart in mtdpercentlist)
{
mtdpercent += StringUtility.GetDecimalFromString(depart.Attributes["tatol"].Value, 0);
mtdprofitpercent += StringUtility.GetDecimalFromString(depart.Attributes["profit"].Value, 0);
}
}
titel = titel.Replace("$mtdpre$", (mtd / 10000).ToString("0.00"));
mtdpercent = ((mtd - mtdpercent) / mtdpercent) * 100;
titel = titel.Replace("$mtdpercent$", mtdpercent.ToString("0.00"));
sheet1.Cells[1, 2] = titel;
content = titel + "<br/>";
titel = sheet1.Cells[2, 2].text;
titel = titel.Replace("$profitpre$", (profitpre / 10000).ToString("0.00"));
titel = titel.Replace("$mtdprofit$", (mtdprofit / 10000).ToString("0.00"));
mtdprofitpercent = ((mtdprofit - mtdprofitpercent) / mtdprofitpercent) * 100;
titel = titel.Replace("$mtdprofitpercent$", mtdprofitpercent.ToString("0.00"));
sheet1.Cells[2, 2] = titel;
content += titel;
fileName = sheet1.Cells[3, 1].text.Replace("$datepre$", date.AddDays(-1).ToString("yyyyMMdd"));
sheet1.Cells[3, 1] = fileName;
#endregion
#region
//64 i
int row = 64;
int col = 9;
DateTime startDate = date.AddDays(-61);
DateTime endDate = date.AddDays(-1);
int daysInMonth = 0;
if (startDate.Month != preMonth)
{
XmlDocument threeDoc = RunXML.GetData(new DateTime(preYear, preMonth, 1));
daysInMonth = DateTime.DaysInMonth(startDate.Year, startDate.Month);
for (int i = startDate.Day; i <= daysInMonth; i++)
{
decimal pertatol = 0;
XmlNodeList nodeList = threeDoc.SelectNodes("/root/item[@date='" + (i < 10 ? "0" + i : i.ToString()) + "']/depart");
foreach (XmlNode item in nodeList)
{
pertatol += StringUtility.GetDecimalFromString(item.Attributes["tatol"].Value, 0);
}
sheet1.Cells[row, col] = new DateTime(startDate.Year, startDate.Month, i).ToString("yyyy-MM-dd");
sheet1.Cells[row, col + 1] = pertatol.ToString("0.00");
row++;
}
}
daysInMonth = DateTime.DaysInMonth(preYear, preMonth);
for (int i = 1; i <= daysInMonth; i++)
{
decimal pertatol = 0;
XmlNodeList nodeList = previousDoc.SelectNodes("/root/item[@date='" + (i < 10 ? "0" + i : i.ToString()) + "']/depart");
foreach (XmlNode item in nodeList)
{
pertatol += StringUtility.GetDecimalFromString(item.Attributes["tatol"].Value, 0);
}
sheet1.Cells[row, col] = new DateTime(preYear, preMonth, i).ToString("yyyy-MM-dd");
sheet1.Cells[row, col + 1] = pertatol.ToString("0.00");
row++;
}
daysInMonth = endDate.Day;
for (int i = 1; i <= daysInMonth; i++)
{
decimal pertatol = 0;
XmlNodeList nodeList = currentDoc.SelectNodes("/root/item[@date='" + (i < 10 ? "0" + i : i.ToString()) + "']/depart");
foreach (XmlNode item in nodeList)
{
pertatol += StringUtility.GetDecimalFromString(item.Attributes["tatol"].Value, 0);
}
sheet1.Cells[row, col] = new DateTime(currentYear, currentMonth, i).ToString("yyyy-MM-dd");
sheet1.Cells[row, col + 1] = pertatol.ToString("0.00");
row++;
}
#endregion
#region top10
int rowCount = 10;
object[,] tatolData = new object[rowCount, 12];
//object[,] descData = new object[rowCount, 12];
object[,] profitData = new object[rowCount, 12];
object[,] costData = new object[rowCount, 12];
for (int i = 0; i < rowCount; i++)
{
tatolData[i, 0] = i + 1;
TeamOrder teamtemp = tatolTop[i];
tatolData[i, 1] = teamtemp.City;
tatolData[i, 2] = teamtemp.Industry;
tatolData[i, 3] = teamtemp.Content;
tatolData[i, 10] = teamtemp.Total.ToString("0.00");
tatolData[i, 11] = teamtemp.TeamID;
//descData[i, 0] = i + 1;
//teamtemp = descTop[i];
//descData[i, 1] = teamtemp.City;
//descData[i, 2] = teamtemp.Industry;
//descData[i, 3] = teamtemp.Content;
//descData[i, 10] = teamtemp.Total.ToString("0.00");
//descData[i, 11] = teamtemp.TeamID;
profitData[i, 0] = i + 1;
teamtemp = ProfitTop[i];
profitData[i, 1] = teamtemp.City;
profitData[i, 2] = teamtemp.Industry;
profitData[i, 3] = teamtemp.Content;
profitData[i, 10] = teamtemp.Profit.ToString("0.00");
profitData[i, 11] = teamtemp.TeamID;
costData[i, 0] = i + 1;
teamtemp = costTop[i];
costData[i, 1] = teamtemp.City;
costData[i, 2] = teamtemp.Industry;
costData[i, 3] = teamtemp.Content;
costData[i, 10] = teamtemp.Cost.ToString("0.00");
costData[i, 11] = teamtemp.TeamID;
}
Range r = sheet1.Range[sheet1.Cells[129, 3], sheet1.Cells[138, 14]];
r.Value2 = tatolData;
//r = sheet1.Range[sheet1.Cells[142, 3], sheet1.Cells[151, 14]];
//r.Value2 = descData;
r = sheet1.Range[sheet1.Cells[142, 3], sheet1.Cells[151, 14]];
r.Value2 = profitData;
r = sheet1.Range[sheet1.Cells[155, 3], sheet1.Cells[164, 14]];
r.Value2 = costData;
#endregion
Worksheet sheet2 = excelApp.Worksheets[2];
#region current month
rowCount = DateTime.DaysInMonth(currentYear, currentMonth);
object[,] dataArray = new object[rowCount, 13];
for (int i = 0; i < rowCount; i++)
{
string col1 = new DateTime(currentYear, currentMonth, i + 1).ToString("yyyy-MM-dd");
dataArray[i, 0] = col1;
if (i < date.AddDays(-1).Day)
{
XmlNode item = currentDoc.SelectSingleNode("/root/item[@date='" + ((i + 1) < 10 ? "0" + (i + 1) : (i + 1).ToString()) + "']");
if (item != null)
{
XmlNode depart = item.SelectSingleNode("depart[@id='0']");
decimal tatol = 0;
decimal profit = 0;
decimal cost = 0;
if (depart != null)
{
dataArray[i, 2] = depart.Attributes["tatol"].Value;
dataArray[i, 6] = depart.Attributes["profit"].Value;
dataArray[i, 10] = depart.Attributes["cost"].Value;
tatol += decimal.Parse(depart.Attributes["tatol"].Value);
profit += decimal.Parse(depart.Attributes["profit"].Value);
cost += decimal.Parse(depart.Attributes["cost"].Value);
}
depart = item.SelectSingleNode("depart[@id='360']");
if (depart != null)
{
dataArray[i, 3] = depart.Attributes["tatol"].Value;
dataArray[i, 7] = depart.Attributes["profit"].Value;
dataArray[i, 11] = depart.Attributes["cost"].Value;
tatol += decimal.Parse(depart.Attributes["tatol"].Value);
profit += decimal.Parse(depart.Attributes["profit"].Value);
cost += decimal.Parse(depart.Attributes["cost"].Value);
}
depart = item.SelectSingleNode("depart[@id='361']");
if (depart != null)
{
dataArray[i, 4] = depart.Attributes["tatol"].Value;
dataArray[i, 8] = depart.Attributes["profit"].Value;
dataArray[i, 12] = depart.Attributes["cost"].Value;
tatol += decimal.Parse(depart.Attributes["tatol"].Value);
profit += decimal.Parse(depart.Attributes["profit"].Value);
cost += decimal.Parse(depart.Attributes["cost"].Value);
}
dataArray[i, 1] = tatol.ToString("0.00");
dataArray[i, 5] = profit.ToString("0.00");
dataArray[i, 9] = cost.ToString("0.00");
}
}
}
r = sheet2.Range[sheet2.Cells[2, 1], sheet2.Cells[rowCount + 1, 13]];
r.Value2 = dataArray;
#endregion
Worksheet sheet3 = excelApp.Worksheets[3];
#region pre month
rowCount = DateTime.DaysInMonth(preYear, preMonth);
dataArray = new object[rowCount, 5];
for (int i = 0; i < rowCount; i++)
{
string col1 = new DateTime(preYear, preMonth, i + 1).ToString("yyyy-MM-dd");
dataArray[i, 0] = col1;
XmlNode item = previousDoc.SelectSingleNode("/root/item[@date='" + ((i + 1) < 10 ? "0" + (i + 1) : (i + 1).ToString()) + "']");
if (item != null)
{
XmlNode depart = item.SelectSingleNode("depart[@id='0']");
decimal tatol = 0;
decimal profit = 0;
decimal cost = 0;
if (depart != null)
{
tatol += decimal.Parse(depart.Attributes["tatol"].Value);
profit += decimal.Parse(depart.Attributes["profit"].Value);
cost += decimal.Parse(depart.Attributes["cost"].Value);
}
depart = item.SelectSingleNode("depart[@id='360']");
if (depart != null)
{
tatol += decimal.Parse(depart.Attributes["tatol"].Value);
profit += decimal.Parse(depart.Attributes["profit"].Value);
cost += decimal.Parse(depart.Attributes["cost"].Value);
}
depart = item.SelectSingleNode("depart[@id='361']");
if (depart != null)
{
tatol += decimal.Parse(depart.Attributes["tatol"].Value);
profit += decimal.Parse(depart.Attributes["profit"].Value);
cost += decimal.Parse(depart.Attributes["cost"].Value);
}
dataArray[i, 1] = tatol.ToString("0.00");
dataArray[i, 3] = profit.ToString("0.00");
dataArray[i, 4] = cost.ToString("0.00");
}
}
r = sheet3.Range[sheet3.Cells[2, 1], sheet3.Cells[rowCount + 1, 4]];
r.Value2 = dataArray;
#endregion
Worksheet sheet4 = excelApp.Worksheets[4];
Worksheet sheet5 = excelApp.Worksheets[5];
#region city
rowCount = DateTime.DaysInMonth(currentYear, currentMonth);
for (int i = 0; i < rowCount; i++)
{
sheet4.Cells[i + 5, 1] = new DateTime(currentYear, currentMonth, i + 1).ToString("yyyy-MM-dd");
sheet5.Cells[i + 5, 1] = new DateTime(currentYear, currentMonth, i + 1).ToString("yyyy-MM-dd");
XmlNode item = currentDoc.SelectSingleNode("/root/item[@date='" + ((i + 1) < 10 ? "0" + (i + 1) : (i + 1).ToString()) + "']");
if (i < date.AddDays(-1).Day && item != null)
{
int j = 0;
while (true)
{
string cityName = ("" + sheet4.Cells[1, j + 2].text);
if (string.IsNullOrEmpty(cityName.Trim())) break;
XmlNode city = item.SelectSingleNode("city[@name='" + cityName.Trim() + "']");
if (city != null)
{
sheet4.Cells[i + 5, j + 2] = city.Attributes["tatol"].Value;
}
cityName = ("" + sheet5.Cells[1, j + 2].text);
if (string.IsNullOrEmpty(cityName.Trim())) break;
city = item.SelectSingleNode("city[@name='" + cityName.Trim() + "']");
if (city != null)
{
sheet5.Cells[i + 5, j + 2] = city.Attributes["profit"].Value;
}
j++;
}
}
}
#endregion
#region img
ChartObject chart1 = (ChartObject)sheet1.ChartObjects(3);
string imagePath = Common.AttImagePath + "chart1.png";
chart1.Chart.Export(imagePath,"PNG",false);
Range range1 = sheet1.Range[sheet1.Cells[46, 1], sheet1.Cells[60, 8]];
Image image1 = getImgage(range1);
string image1Path = Common.AttImagePath + "image1.png";
image1.Save(image1Path, System.Drawing.Imaging.ImageFormat.Gif);
image1.Dispose();
Range range2 = sheet1.Range[sheet1.Cells[126, 1], sheet1.Cells[164, 14]];
Image image2 = getImgage(range2);
string image2Path = Common.AttImagePath + "image2.png";
image2.Save(image2Path, System.Drawing.Imaging.ImageFormat.Gif);
image2.Dispose();
ec.Atts.Add(new EmailAtt("chart1.png", imagePath, "$chart1$"));
ec.Atts.Add(new EmailAtt("image1.png", image1Path, "$image1$"));
ec.Atts.Add(new EmailAtt("image2.png", image2Path, "$image2$"));
#endregion
}
catch (Exception e)
{
Log.LogRun(string.Format("{0}->build excel error", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
Log.LogError(string.Format("build excel start->", e.Message));
throw e;
}
finally
{
ExcelHelper.Save(excelApp);
Log.LogRun(string.Format("{0}->build excel end", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
}
ec.Atts.Insert(0,new EmailAtt(fileName + ".xlsx", Common.TempPath.Replace("$date$", date.AddDays(-1).ToString("yyyyMMdd"))));
ec.Content = content;
ec.Titel = fileName;
return ec;
}
public static Image getImgage(Range area)
{
Image img = null;
if (area != null)
{
area.Copy(Type.Missing);
img = Clipboard.GetImage();
}
return img;
}
}
}