今天把我之前做的东西拿出来又看了看,这是之前的一个Bug自己也明白原因大概知道问题出现在哪,但是由于自己接触的少,还是没整出来
excel1.Visible = true;放在服务器上报错报这种错误,由于当时自己在本机上做的测试都好好地,然后兴奋地放到服务器上发布了,谁知道报了错误,开始报80000错误后来解决了,中间报了好几种错误自己都给解决了,但是最后报的这个错误没解决,上面那句代码好像是不支持在服务器上直接打开,按照道理应该是先下载,然后保存本机上,才可以打开,思路是这样的正常。但是我很纳闷为什么我在自己本机上做测试都可以,本机相当于自己的服务器。放到服务器上为什么不可以支持客户端直接访问到服务器上,把资源下载到客户自己电脑上,然后打开。
剩下的解决方案自己没解决,所以请高手给我指点下,先谢谢大家!
string id = Convert.ToString(Session["userid"]);
Page.RegisterStartupScript("alert", "<script language=javascript>alert('登陆超时,请重新登陆!'); window.parent.location='Index.aspx';</script>");
return;
}
int xh = 0;
if (txtXH.Text.Trim() != "")
{
xh = Convert.ToInt32(txtXH.Text.Trim().ToString());
}
else
{
xh = Convert.ToInt32(Request.QueryString["xh"]);
}
//获取总共记录多少条
string sql = string.Format("select count(*) from YK_YPCGMX where ghdw_id= '{0}' and cgxh='{1}'", id, xh);
// conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
int count = Convert.ToInt32(cmd.ExecuteScalar());
Application excel1 = new Application();
Workbook workbook1 = excel1.Workbooks.Add(true);
Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];
//控制Excel表中要显示的列行 (count控制列数)
Range range1 = worksheet1.get_Range("A1", "D" + (count + 1));//选择操作块
// range1.Font.Bold = true;//设置黑体
// range1.Font.Size = 18;//设置字体大小
//range1.Font.Name = "仿宋";//设置字体
//range1.Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Blue);//设置字体颜色
range1.HorizontalAlignment = XlHAlign.xlHAlignCenter;//设置水平对齐方式
range1.VerticalAlignment = XlVAlign.xlVAlignCenter;//设置垂直对齐方式
//填充Excel
// range1.Value2 = ds;
worksheet1.Cells[1, 1] = "订单号";
worksheet1.Cells[1, 2] = "药品idm";
worksheet1.Cells[1, 3] = "药品名称";
worksheet1.Cells[1, 4] = "药品规格";
worksheet1.Cells[1, 5] = "药品代码";
worksheet1.Cells[1, 6] = "药品单位";
worksheet1.Cells[1, 7] = "采购数量";
worksheet1.Cells[1, 8] = "供货单位名称";
worksheet1.Cells[1, 9] = "科室代码";
worksheet1.Cells[1, 10] = "科室名称";
worksheet1.Cells[1, 11] = "厂家名称";
string sql1 = string.Format("select cgxh as xh,cd_idm as 药品idm,A.ypmc as 药品名称,A.ypgg as 药品规格, A.ypdm as 药品代码, A.ykdw as 单位, Convert(decimal(18,1),cgsl/A.ykxs) as 数量 ,A.ghdw_mc AS 供商名称,A.ghdw_id as 代码,ksdm as 科室代码, name as 科室名称,cjmc as 厂家名称 from YK_YPCGMX AS A inner join YK_YPCGZD AS B ON A.cgxh=B.xh inner join YF_YFDMK AS C on C.id=B.ksdm inner join YK_YPCDMLK AS D ON A.cd_idm=D.idm where A.ghdw_id='{0}' and A.cgxh='{1}'", id, xh);//查询语句
DataSet ds1 = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(sql1, conn);
sda.Fill(ds1, "ds");
System.Data.DataTable dt = ds1.Tables[0];
//循环输出每条记录
for (int i = 2; i < count + 2; i++)
{
worksheet1.Cells[i, 1] = dt.Rows[i - 2][0];
worksheet1.Cells[i, 2] = dt.Rows[i - 2][1];
worksheet1.Cells[i, 3] = dt.Rows[i - 2][2];
worksheet1.Cells[i, 4] = dt.Rows[i - 2][3];
worksheet1.Cells[i, 5] = dt.Rows[i - 2][4];
worksheet1.Cells[i, 6] = dt.Rows[i - 2][5];
worksheet1.Cells[i, 7] = dt.Rows[i - 2][6];
worksheet1.Cells[i, 8] = dt.Rows[i - 2][7];
worksheet1.Cells[i, 9] = dt.Rows[i - 2][8];
worksheet1.Cells[i, 10] = dt.Rows[i - 2][9];
worksheet1.Cells[i, 11] = dt.Rows[i - 2][10];
}
range1.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = XlLineStyle.xlContinuous;
range1.Borders.get_Item(XlBordersIndex.xlEdgeRight).LineStyle = XlLineStyle.xlContinuous;
range1.Borders.get_Item(XlBordersIndex.xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous;
range1.Borders.get_Item(XlBordersIndex.xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous;
//也可用后面的代码代替上面四项range1.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic,null);
range1.Borders.get_Item(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous;//块内竖线
range1.Borders.get_Item(XlBordersIndex.xlInsideVertical).LineStyle = XlLineStyle.xlContinuous;//块内横线
// excel1.Save("aaa.xls");
excel1.Visible = true;
conn.Close();