//调用函数
protected void Save_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtBeginTime.Value))
{
string[] str = txtBeginTime.Value.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
y = str[0];
m = str[1];
}
DataTable tdSet = Report_B.Instance.GetMonthInventories_Tablr(y, m, Obj_Search.Value);
CreateExcel(tdSet);
}
//Excel表格格式设置
public void CreateExcel(DataTable ds)
{
Response.AddHeader("Content-Disposition", "attachment;filename=" +
HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls", System.Text.Encoding.UTF8));
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
int i = 0;
System.IO.StringWriter sw = new System.IO.StringWriter();
sw.WriteLine("<?xml version=\"1.0\"?>");
sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw.WriteLine("xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sw.WriteLine("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
sw.WriteLine("xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw.WriteLine("xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sw.WriteLine("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
sw.WriteLine("<Created>1996-12-17T01:32:42Z</Created>");
sw.WriteLine("<LastSaved>2012-07-28T01:05:10Z</LastSaved>");
sw.WriteLine("<Version>11.9999</Version>");
sw.WriteLine("</DocumentProperties>");
sw.WriteLine("<OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\">");
sw.WriteLine("<RemovePersonalInformation/>");
sw.WriteLine("</OfficeDocumentSettings>");
sw.WriteLine("<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine("<WindowHeight>4530</WindowHeight>");
sw.WriteLine("<WindowWidth>8505</WindowWidth>");
sw.WriteLine("<WindowTopX>480</WindowTopX>");
sw.WriteLine("<WindowTopY>120</WindowTopY>");
sw.WriteLine("<AcceptLabelsInFormulas/>");
sw.WriteLine("<ProtectStructure>False</ProtectStructure>");
sw.WriteLine("<ProtectWindows>False</ProtectWindows>");
sw.WriteLine("</ExcelWorkbook>");
sw.WriteLine("<Styles>");
sw.WriteLine("<Style ss:ID=\"Default\" ss:Name=\"Normal\">");
sw.WriteLine("<Alignment ss:Vertical=\"Bottom\"/>");
sw.WriteLine("<Borders/>");
sw.WriteLine("<Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"10\"/>");
sw.WriteLine("<Interior/>");
sw.WriteLine("<NumberFormat/>");
sw.WriteLine("<Protection/>");
sw.WriteLine("</Style>");
sw.WriteLine("<Style ss:ID=\"s21\">");
sw.WriteLine("<Font ss:FontName=\"宋体\" x:CharSet=\"134\"/>");
sw.WriteLine("</Style>");
sw.WriteLine("<Style ss:ID=\"s24\">");
sw.WriteLine("<Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Bottom\"/>");
sw.WriteLine("<Font ss:FontName=\"宋体\" x:CharSet=\"134\"/>");
sw.WriteLine("</Style>");
sw.WriteLine("</Styles>");
sw.WriteLine("<Worksheet ss:Name=\"月盘点报表\">");
sw.WriteLine("<Table x:FullColumns=\"1\"");
sw.WriteLine("x:FullRows=\"1\" ss:DefaultColumnWidth=\"100\" ss:DefaultRowHeight=\"14.25\">");
sw.WriteLine("<Column ss:Index=\"2\" ss:StyleID=\"s24\" ss:AutoFitWidth=\"0\" ss:Width=\"50.25\"/>");
sw.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"70.5\"/>");
sw.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"68.25\"/>");
sw.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"70.5\"/>");
sw.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"71.25\" ss:Span=\"1\"/>");
sw.WriteLine("<Column ss:Index=\"8\" ss:AutoFitWidth=\"0\" ss:Width=\"62.25\"/>");
sw.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"63\"/>");
sw.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"62.25\"/>");
sw.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"62.25\"/>");
sw.WriteLine("<Column ss:AutoFitWidth=\"0\" ss:Width=\"72\" ss:Span=\"1\"/>");
sw.WriteLine("<Column ss:Index=\"14\" ss:AutoFitWidth=\"0\" ss:Width=\"71.25\"/>");
sw.WriteLine("<Row ss:Height=\"12\" ss:StyleID=\"s24\">");
sw.WriteLine("<Cell><Data ss:Type=\"String\">物品名称</Data></Cell>");
sw.WriteLine("<Cell><Data ss:Type=\"String\">单位</Data></Cell>");
sw.WriteLine("<Cell><Data ss:Type=\"String\">期初余额(数量)</Data></Cell>");
sw.WriteLine("<Cell><Data ss:Type=\"String\">期初余额(金额)</Data></Cell>");
sw.WriteLine("<Cell><Data ss:Type=\"String\">本月购入(数量)</Data></Cell>");
sw.WriteLine("<Cell><Data ss:Type=\"String\">本月购入(单价)</Data></Cell>");
sw.WriteLine("<Cell><Data ss:Type=\"String\">本月购入(金额)</Data></Cell>");
sw.WriteLine("<Cell><Data ss:Type=\"String\">本月销售数量</Data></Cell>");
sw.WriteLine("<Cell><Data ss:Type=\"String\">本月销售金额</Data></Cell>");
sw.WriteLine("<Cell><Data ss:Type=\"String\">销售成本</Data></Cell>");
sw.WriteLine("<Cell><Data ss:Type=\"String\">毛利润</Data></Cell>");
sw.WriteLine("<Cell><Data ss:Type=\"String\">期末结余(数量)</Data></Cell>");
sw.WriteLine("<Cell><Data ss:Type=\"String\">期末结余(单价)</Data></Cell>");
sw.WriteLine("<Cell><Data ss:Type=\"String\">期末结余(金额)</Data></Cell>");
sw.WriteLine("</Row>");
foreach (DataRow row in ds.Select())
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
sw.WriteLine("<Row>");
for (i = 1; i < ds.Columns.Count; i++)
{
if (i ==1|| i == 2)
{
sw.WriteLine("<Cell><Data ss:Type=\"String\">" + row[i].ToString() + "</Data></Cell>");
}
else
{
sw.WriteLine("<Cell><Data ss:Type=\"Number\">" + row[i].ToString() + "</Data></Cell>");
}
}
sw.WriteLine("</Row>");
}
int r = ds.Rows.Count + 2;
if (ds.Rows.Count > 0)
{
sw.WriteLine("<Row ss:Index=\"" + r + "\" ss:AutoFitHeight=\"0\">");
sw.WriteLine("<Cell><Data ss:Type=\"String\">合计:</Data></Cell>");
sw.WriteLine("<Cell ss:Index=\"3\" ss:Formula=\"=SUM(R[-" + ds.Rows.Count + "]C:R[-1]C)\"><Data ss:Type=\"Number\">0</Data></Cell>");
sw.WriteLine("<Cell ss:Index=\"4\" ss:Formula=\"=SUM(R[-" + ds.Rows.Count + "]C:R[-1]C)\"><Data ss:Type=\"Number\">0</Data></Cell>");
sw.WriteLine("<Cell ss:Index=\"5\" ss:Formula=\"=SUM(R[-" + ds.Rows.Count + "]C:R[-1]C)\"><Data ss:Type=\"Number\">0</Data></Cell>");
sw.WriteLine("<Cell ss:Index=\"6\" ss:Formula=\"=SUM(R[-" + ds.Rows.Count + "]C:R[-1]C)\"><Data ss:Type=\"Number\">0</Data></Cell>");
sw.WriteLine("<Cell ss:Index=\"7\" ss:Formula=\"=SUM(R[-" + ds.Rows.Count + "]C:R[-1]C)\"><Data ss:Type=\"Number\">0</Data></Cell>");
sw.WriteLine("<Cell ss:Index=\"8\" ss:Formula=\"=SUM(R[-" + ds.Rows.Count + "]C:R[-1]C)\"><Data ss:Type=\"Number\">0</Data></Cell>");
sw.WriteLine("<Cell ss:Index=\"9\" ss:Formula=\"=SUM(R[-" + ds.Rows.Count + "]C:R[-1]C)\"><Data ss:Type=\"Number\">0</Data></Cell>");
sw.WriteLine("<Cell ss:Index=\"10\" ss:Formula=\"=SUM(R[-" + ds.Rows.Count + "]C:R[-1]C)\"><Data ss:Type=\"Number\">0</Data></Cell>");
sw.WriteLine("<Cell ss:Index=\"11\" ss:Formula=\"=SUM(R[-" + ds.Rows.Count + "]C:R[-1]C)\"><Data ss:Type=\"Number\">0</Data></Cell>");
sw.WriteLine("<Cell ss:Index=\"12\" ss:Formula=\"=SUM(R[-" + ds.Rows.Count + "]C:R[-1]C)\"><Data ss:Type=\"Number\">0</Data></Cell>");
sw.WriteLine("<Cell ss:Index=\"13\" ss:Formula=\"=SUM(R[-" + ds.Rows.Count + "]C:R[-1]C)\"><Data ss:Type=\"Number\">0</Data></Cell>");
sw.WriteLine("<Cell ss:Index=\"14\" ss:Formula=\"=SUM(R[-" + ds.Rows.Count + "]C:R[-1]C)\"><Data ss:Type=\"Number\">0</Data></Cell>");
sw.WriteLine("</Row>");
}
else
{
sw.WriteLine("<Row ss:Index=\"" + r + "\" ss:AutoFitHeight=\"0\">");
sw.WriteLine("<Cell><Data ss:Type=\"String\">暂无数据...</Data></Cell>");
sw.WriteLine("</Row>");
}
sw.WriteLine("</Table>");
sw.WriteLine("<WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine("<Print>");
sw.WriteLine("<ValidPrinterInfo/>");
sw.WriteLine("<PaperSizeIndex>9</PaperSizeIndex>");
sw.WriteLine("<HorizontalResolution>100</HorizontalResolution>");
sw.WriteLine("<VerticalResolution>100</VerticalResolution>");
sw.WriteLine("</Print>");
sw.WriteLine("<Selected/>");
sw.WriteLine("<Panes>");
sw.WriteLine("<Pane>");
sw.WriteLine("<Number>3</Number>");
sw.WriteLine("<ActiveRow>1</ActiveRow>");
sw.WriteLine("</Pane>");
sw.WriteLine("</Panes>");
sw.WriteLine("<ProtectObjects>False</ProtectObjects>");
sw.WriteLine("<ProtectScenarios>False</ProtectScenarios>");
sw.WriteLine("</WorksheetOptions>");
sw.WriteLine(" </Worksheet>");
sw.WriteLine("</Workbook>");
Response.Write(sw);
Response.End();
}