我编写了一个生成Excel报告的应用程序。该报告具有120列和100行。我唯一需要打印的是针对经销商的产品数量。产品打印为列,分发者打印为行。
以下是我完整的代码。
以表格格式
custoMeth cls = new custoMeth();
//Clear
drill.Clear();
distTbl.Clear();
monthList.Clear();
//Declarations
divCode = int.Parse(cmbDivCode.SelectedItem.ToString());
catName = cmbCategory.SelectedItem.ToString();
distName = cmbDistributor.SelectedItem.ToString();
fromDT = dtFrom.Text.ToString();
toDT = dtTo.Text.ToString();
monthList = cls.monthsList(this.dtFrom, this.dtTo);
string month = cls.selectedmonth(this.dtFrom);
// load excel, and create a new workbook
Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbooks.Add();
// single worksheet
Microsoft.Office.Interop.Excel._Worksheet Worksheet = Excel.ActiveSheet;
// Custom Header
Worksheet.Range[Worksheet.Cells[1, 1], Worksheet.Cells[1, 8]].Merge();
Worksheet.Range[Worksheet.Cells[1, 1], Worksheet.Cells[2, 8]].Merge();
Worksheet.Range[Worksheet.Cells[3, 1], Worksheet.Cells[3, 3]].Merge();
cls.excelHeader(Worksheet, 3, 1, "Generated Date : " + System.DateTime.Today.ToShortDateString() + "", true, "none", "Left", "none", 0);
Worksheet.Range[Worksheet.Cells[4, 1], Worksheet.Cells[4, 3]].Merge();
cls.excelHeader(Worksheet, 4, 1, "Sound And FreeIssue Report (Division Code : " + divCode + ")", true, "none", "Left", "none", 0);
Worksheet.Range[Worksheet.Cells[5, 1], Worksheet.Cells[5, 3]].Merge();
cls.excelHeader(Worksheet, 5, 1, ("Report Date: " + fromDT + " To " + toDT), true, "none", "Left", "none", 0);
Worksheet.Range[Worksheet.Cells[6, 1], Worksheet.Cells[6, 3]].Merge();
cls.excelHeader(Worksheet, 6, 1, "Category: " + catName + "", true, "none", "Left", "none", 0);
cls.excelHeader(Worksheet, 8, 1, "Distributor", true, "none", "Left", "none", 20);
int row = 8;
int col = 2;
#region Print Product Names
if (catName == "All")
{
//sqlData = "SELECT DISTINCT Commission, ProductName FROM Drill INNER JOIN Product ON Drill.Commission = Product.CommissionCode AND Drill.DivCode = Product.DivCode ";
//sqlData = sqlData + "WHERE TRMM = '" + month + "' AND Drill.DivCode = '" + divCode + "' AND Commission NOT IN ('xxx') ORDER BY Commission";
sqlData = "SELECT DISTINCT Commission FROM Drill WHERE TRMM = '" + month + "' AND Drill.DivCode = '" + divCode + "' ORDER BY Commission";
comTBL = cls.data(sqlData, myConnection, comTBL);
prBar.Maximum = comTBL.Rows.Count;
for (int i = 0; i < comTBL.Rows.Count; i++)
{
string sqlPrd = "SELECT ProductName FROM Product WHERE CommissionCode = '" + comTBL.Rows[i]["Commission"].ToString() + "' AND DivCode = '" + divCode + "'";
string productName = cls.GetValue<string>(sqlPrd, myConnection);
cls.excelHeader(Worksheet, row, col, productName, true, "none", "none", "Bottom", 5, 90); //comTBL.Rows[i]["ProductName"].ToString()
col = col + 1;
prBar.Value = i;
}
}
else
{
sqlData = "SELECT DISTINCT Commission FROM Drill WHERE RCateg = '" + catName + "' AND TRMM = '" + month + "' ORDER BY Commission";
comTBL = cls.data(sqlData, myConnection, comTBL);
prBar.Maximum = comTBL.Rows.Count;
for (int i = 0; i < comTBL.Rows.Count; i++)
{
string sqlPrd = "SELECT ProductName FROM Product WHERE CommissionCode = '" + comTBL.Rows[i]["Commission"].ToString() + "' AND DivCode = '" + divCode + "'";
string productName = cls.GetValue<string>(sqlPrd, myConnection);
cls.excelHeader(Worksheet, row, col, productName, true, "none", "none", "Bottom", 5, 90);
col = col + 1;
prBar.Value = i;
}
}
#endregion
row = 9;
col = 1;
#region Distributor
if (cmbDistributor.SelectedItem.ToString() == "All")
{
string sqlDist = "SELECT DISTINCT StkCd FROM Drill WHERE DivCode = '" + divCode + "' AND TRMM = '" + month + "'";
distTbl = cls.data(sqlDist, myConnection, distTbl);
}
else
{
string sqlDistCode = "SELECT Code FROM Distributors WHERE Name = '" + distName + "' AND Active IN ('True') AND DivCode = '" + divCode + "'";
distCode = cls.GetValue<string>(sqlDistCode, myConnection);
string sqlDist = "SELECT DISTINCT StkCd FROM Drill WHERE DivCode = '" + divCode + "' AND StkCd = '" + distCode + "' AND TRMM = '" + month + "'";
distTbl = cls.data(sqlDist, myConnection, distTbl);
}
#endregion
prBar.Value = 0;
prBar.Maximum = distTbl.Rows.Count;
for (int j = 0; j < distTbl.Rows.Count; j++)
{
col = 1;
string sqlDistName = "SELECT Name FROM Distributors WHERE Active IN ('True') AND Code = '" + distTbl.Rows[j]["StkCd"].ToString() + "' AND DivCode = '" + divCode + "'";
distName = cls.GetValue<string>(sqlDistName, myConnection);
cls.excelHeader(Worksheet, row, col, distName, true, "none", "Left", "none", 20);
col = 2;
prBar1.Maximum = comTBL.Rows.Count;
for (int p = 0; p < comTBL.Rows.Count; p++)
{
sqlDmg = "SELECT StkCd, SUM(Qty) AS Qty FROM Drill ";
sqlDmg = sqlDmg + "WHERE StkCd = '" + distTbl.Rows[j]["StkCd"].ToString() + "' AND TRMM = '" + month + "' AND Commission = '" + comTBL.Rows[p]["Commission"].ToString() + "' AND PrtID IN ('D') AND DivCode = '" + divCode + "'";
sqlDmg = sqlDmg + "GROUP BY StkCd";
drill = cls.data(sqlDmg, myConnection, drill);
int qty = 0;
if (drill.Rows.Count > 0)
{ qty = int.Parse(drill.Rows[0]["Qty"].ToString()); }
else
{ qty = 0; }
qty = (qty * -1);
cls.excelHeader(Worksheet, row, col, qty);
col = col + 1;
prBar1.Value = p;
}
row = row + 1;
prBar.Value = j;
}
Excel.Visible = true;
cls文件
public void excelHeader(Microsoft.Office.Interop.Excel._Worksheet Worksheet, int row, int column, string value, Boolean bold, string size, string hAlign, string vAlign, int colWidth, int orientation)
{
Worksheet.Cells[row, column].Value = value;
Worksheet.Cells[row, column].Font.Bold = bold;
if (size != "none")
{ Worksheet.Cells[row, column].Font.Size = size; }
if (colWidth != 0)
{ Worksheet.Columns[column].ColumnWidth = colWidth; }
if (hAlign == "Left")
{ Worksheet.Cells[row, column].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; }
else if (hAlign == "Center")
{ Worksheet.Cells[row, column].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; }
else if (hAlign == "Right")
{ Worksheet.Cells[row, column].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight; }
else
{ Worksheet.Cells[row, column].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignGeneral; }
if (vAlign == "Left")
{ Worksheet.Cells[row, column].VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; }
else if (vAlign == " Center")
{ Worksheet.Cells[row, column].VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; }
else if (vAlign == "Right")
{ Worksheet.Cells[row, column].VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight; }
else
{ Worksheet.Cells[row, column].VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignGeneral; }
Worksheet.Cells[row, column].Orientation = orientation;
}
public DataTable data(string sql, SqlConnection conn, DataTable dt)
{
dt.Clear();
SqlCommand queryCommand = new SqlCommand(sql, conn);
SqlDataAdapter adapter = new SqlDataAdapter(queryCommand);
adapter.Fill(dt);
return dt;
}
此代码有效,但是花费太多时间。有时,它给我错误“超时已到期。在完成前已超时或服务器没有响应”。 请帮忙。 谢谢。