110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace Personal
{
public class Print
{
/// <summary>
/// 将DataTabl内容打印出来
/// Table 表, strHead 表名,Zoom 缩放比%
/// </summary>
private void baobiao(DataTable Table, string strHead, sbyte Zoom)
{
try
{
Excel.Application excelKccx = new Excel.Application(); //创建excel对象
Excel._Workbook xBk;
Excel._Worksheet xSt;
xBk = excelKccx.Workbooks.Add(true);
xSt = (Excel._Worksheet)xBk.ActiveSheet;
Excel.Range range = xSt.get_Range(excelKccx.Cells[1, 1], excelKccx.Cells[1, 22]);
range.ClearContents();//先把Range内容清除,合并才不会出错
range.MergeCells = true;
range.Value2 = strHead;//"普通医保住院定额超支管理报表(一般住院)";
range.Font.Bold = true;
range.Font.Size = 15;
range.Font.ColorIndex = 4;//颜色
range.HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; //设置标题格式为居中对齐
// 设置表头
int row = 2;
for (int i = 0; i < Table.Columns.Count; i++)//取字段名
{
excelKccx.Cells[row, i + 1] = Table.Columns[i].ColumnName.ToString();
xSt.get_Range(excelKccx.Cells[row, i + 1], excelKccx.Cells[row, i + 1]).Interior.ColorIndex = 5;
}
xSt.get_Range(excelKccx.Cells[row, 1], excelKccx.Cells[row, Table.Columns.Count]).EntireColumn.AutoFit();//自动调整列宽
xSt.get_Range(excelKccx.Cells[row, 1], excelKccx.Cells[row, Table.Columns.Count]).Font.Bold = true;
for (int i = 0; i < Table.Rows.Count; i++)//取记录值
{
row++;
for (int j = 0; j < Table.Columns.Count; j++)
{
excelKccx.Cells[row, j + 1] = Table.Rows[i][j].ToString();
}
}
excelKccx.Visible = true;
object oMissing = System.Reflection.Missing.Value;
xSt.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA3;//设置纸张大小
xSt.PageSetup.Zoom = Zoom; //缩放比例
xSt.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;//横向打印 xlPortrait1-纵向,2-横向;
xSt.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
xBk.PrintOut(oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); // 打印:
// xBk.PrintPreview(oMissing);//打印预览
xBk.Close(null, null, null);
excelKccx.Workbooks.Close();
excelKccx.Application.Quit();
excelKccx.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelKccx);
GC.Collect();//强行销毁
}
catch
{
GC.Collect();//强行销毁
};
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Export(GetData(), @"D:\aa");
}
}
public System.Data.DataTable GetData()
{
System.Data.DataTable dt = new System.Data.DataTable();
DataColumn dc1 = new DataColumn("Name");
DataColumn dc2 = new DataColumn("Age");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
DataRow dr1 = dt.NewRow();
dr1[0] = "Lily";
dr1[1] = "10";
DataRow dr2 = dt.NewRow();
dr2[0] = "Lucy";
dr2[1] = "9";
dt.Rows.Add(dr1);
dt.Rows.Add(dr2);
return dt;
}
public static void Export(System.Data.DataTable dt, string filePath)
{
if (dt == null)
{
throw new Exception("数据表中无数据");
}
int eRowIndex = 1;
int eColIndex = 1;
int cols = dt.Columns.Count;
int rows = dt.Rows.Count;
Excel.Application xlApp = new Excel.ApplicationClass();
Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
try
{
//列名的处理
for (int i = 0; i < cols; i++)
{
xlApp.Cells[eRowIndex, eColIndex] = dt.Columns[i].ColumnName;
eColIndex++;
}
//列名加粗显示
xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[eRowIndex, cols]).Font.Bold = true;
xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[rows + 1, cols]).Font.Name = "Arial";
xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[rows + 1, cols]).Font.Size = "10";
eRowIndex++;
for (int i = 0; i < rows; i++)
{
eColIndex = 1;
for (int j = 0; j < cols; j++)
{
xlApp.Cells[eRowIndex, eColIndex] = dt.Rows[i][j].ToString();
eColIndex++;
}
eRowIndex++;
}
//控制单元格中的内容。
xlApp.Cells.EntireColumn.AutoFit();
xlApp.DisplayAlerts = false;
xlBook.SaveCopyAs(filePath);
xlApp.Workbooks.Close();
}
catch
{
throw;
}
finally
{
xlApp.Quit();
//杀掉Excel进程。
GC.Collect();
}
}
}
//记得要添加Com+
using Microsoft.Office;
string strInfo = "<table style=\"border:#000000 1px solid;border-collapse:collapse;\" cellSpacing=\"0\" cellPadding=\"0\" align=\"center\" width=\"100%\"><tr><td align=\"center\" style=\"padding:2px 0px 2px 0px;FONT-WEIGHT: bold; FONT-SIZE: 14px\">里程碑描述</td><td align=\"center\" style=\"padding:2px 0px 2px 0px;BORDER-left: rgb(0,0,0) 1px solid; FONT-WEIGHT: bold; FONT-SIZE: 14px\">机组</td><td align=\"center\" style=\"padding:2px 0px 2px 0px;BORDER-left: rgb(0,0,0) 1px solid; FONT-WEIGHT: bold; FONT-SIZE: 14px\">时间</td><td align=\"center\" style=\"padding:2px 0px 2px 0px;BORDER-left: rgb(0,0,0) 1px solid; FONT-WEIGHT: bold; FONT-SIZE: 14px\">完成及预测</td><td align=\"center\" style=\"padding:2px 0px 2px 0px;BORDER-left: rgb(0,0,0) 1px solid; FONT-WEIGHT: bold; FONT-SIZE: 14px\">备注</td></tr>";
//遗留
if(dtMorra.Rows.Count > 0)
{
strInfo += "<tr><td style=\"padding:2px 0px 2px 0px;BORDER-top: rgb(0,0,0) 1px solid;FONT-WEIGHT: bold; FONT-SIZE: 14px\" colSpan=\"5\">遗留</td></tr>";
for(int i = 0;i<dtMorra.Rows.Count;i++)
{
strInfo += "<tr><td style=\"padding:2px 0px 2px 0px;BORDER-top: rgb(0,0,0) 1px solid;\" width=\"45%\">"+dtMorra.Rows[i]["MS_NM"].ToString()+"</td><td style=\"padding:2px 0px 2px 0px;BORDER-top: rgb(0,0,0) 1px solid;BORDER-left: rgb(0,0,0) 1px solid;\" align=\"center\" width=\"10%\">"+dtMorra.Rows[i]["UNIT"].ToString()+"</td><td style=\"padding:2px 0px 2px 0px;BORDER-top: rgb(0,0,0) 1px solid;BORDER-left: rgb(0,0,0) 1px solid;\" align=\"center\" width=\"10%\">"+dtMorra.Rows[i]["PLAN_DATE"].ToString()+"</td><td style=\"padding:2px 0px 2px 0px;BORDER-top: rgb(0,0,0) 1px solid;BORDER-left: rgb(0,0,0) 1px solid;\" width=\"15%\">"+dtMorra.Rows[i]["FINISH_STATE"].ToString()+"</td><td style=\"padding:2px 0px 2px 0px;BORDER-top: rgb(0,0,0) 1px solid;BORDER-left: rgb(0,0,0) 1px solid;\" width=\"20%\">"+dtMorra.Rows[i]["TRACK_MEMO"].ToString()+"</td></tr>";
}
}
strInfo += "</table>";
this.lblDisplay.Text = strInfo;
/// <summary>
/// 导出数据到EXCEL
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
#region private void btnToExcel_Click(object sender, System.EventArgs e)
private void btnToExcel_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312"; //设置了类型为中文防止乱码的出现
string strFileName = DateTime.Now.ToString("yyyy-MM") + HttpUtility.UrlEncode("[一级]遗留、本月完成及未来预测",System.Text.Encoding.UTF8);
Response.AppendHeader("Content-Disposition","attachment;filename=" + strFileName + ".xls"); //定义输出文件和文件名
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为word文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.lblDisplay.RenderControl(oHtmlTextWriter);
string charset ="<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">";//防止导出后的Word文件内容乱码
Response.Write(charset+oStringWriter.ToString());
Response.End();
}
#endregion
Me.Response.ClearContent()
Me.Response.ClearHeaders()
Me.Response.ContentType = "application/vnd.ms-excel"
Me.Response.AddHeader("Content-Disposition", "inline;filename='Test.xls'")
Me.Response.WriteFile("文件所在路径")
Me.Response.End()