Files
15-WeiChai-MES_Manage/App_code/CommonFunction.cs

116 lines
4.4 KiB
C#

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
/// <summary>
///CommonFunction 的摘要说明
/// </summary>
public partial class CommonFunction
{
public CommonFunction()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 保持GridView状态
/// </summary>
/// <param name="GridView1"></param>
public static void SetGridViewSelection(GridView GridView1)
{
if (GridView1.Rows.Count > 0)
{
if (GridView1.SelectedIndex != -1)
{
if (GridView1.SelectedIndex >= GridView1.Rows.Count)
GridView1.SelectedIndex = GridView1.Rows.Count - 1;
}
else
GridView1.SelectedIndex = 0;
//去掉gridview的空格
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int j = 1; j < GridView1.Columns.Count-1; j++)
GridView1.Rows[i].Cells[j].Text = GridView1.Rows[i].Cells[j].Text.Trim().Replace("&nbsp;", "");
}
}
else
{
GridView1.SelectedIndex = -1;
}
}
public static void OutPutDataAndPic(string path, DataTable table)
{
//导出excel 时 每个字段宽度 统一 设置为100
int excel_biaoge_width = 170;
//图片的高度
int height = 400;
int with = 800;
//图片所占用的excel表的行数
int excel_tupian_gaodu_hangshu = 22;
string str;
if (table == null) return;
System.Web.HttpContext web = System.Web.HttpContext.Current;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("Sheet.xls", System.Text.Encoding.UTF8).ToString());
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">");
HttpContext.Current.Response.ContentType = "application/ms-excel";
//表头
HttpContext.Current.Response.Write("<table border=1>");
HttpContext.Current.Response.Write("<tr>");
HttpContext.Current.Response.Write("<td align=\"center\" valign=\"middle\" colspan=\"" + table.Columns.Count + "\"></td>");
HttpContext.Current.Response.Write("</tr>");
HttpContext.Current.Response.Write("<tr>");
//string webPath = SPC.WebUI.PageBase.UrlBase;
//webPath = webPath.Remove(webPath.Length - 1, 1);
//str = "<td align=\"center\" valign=\"middle\" rowspan=\"" + excel_tupian_gaodu_hangshu + "\" colspan=\"" + table.Columns.Count + "\">" +
//"<img alt=\"\" src=\"" + path + "\" width=\" " + excel_biaoge_width * table.Columns.Count + " \" height= \" " + height + " \" />" + "</td>";
str = "<td align=\"center\" valign=\"middle\" >" +
// "<img alt=\"\" src=\"" + "/Chart_excel/" + path + "\" width=\" " + with + " \" height= \" " + height + " \" />" + "</td>";
"<img alt=\"\" src=\"" + SPC.WebUI.PageBase.UrlBase + "/Chart_excel/" + path + "\" width=\" " + with + " \" height= \" " + height + " \" />" + "</td>";
HttpContext.Current.Response.Write(str);
HttpContext.Current.Response.Write("</tr>");
for (int i = 0; i < excel_tupian_gaodu_hangshu; i++)
{
HttpContext.Current.Response.Write("<tr>");
}
//输出表字段
for (int i = 0; i < table.Columns.Count; i++)
{
HttpContext.Current.Response.Write("<td width=\"" + excel_biaoge_width + "\" align=\"center\" valign=\"middle\">" + table.Columns[i].ColumnName + "</td>");
}
foreach (DataRow dataRow in table.Rows)
{
HttpContext.Current.Response.Write("<tr>");
for (int i = 0; i < table.Columns.Count; i++)
{
HttpContext.Current.Response.Write("<td>" + dataRow[i] + "</td>");
}
HttpContext.Current.Response.Write("</tr>");
}
HttpContext.Current.Response.Write("</table>");
HttpContext.Current.Response.End();
}
}