0
好的。我從GridView控件使這個令人驚歎的垃圾代碼導出表XLS (但仍然不知道如何使其可編輯(不是隻讀))打印動態GridView內容
現在的任務是打印數據。我可以將它發送到HTML格式(如在XLS上)但與沒有行和下載文件的打印是奇怪的。
如何爲我的GridView製作一些「打印視圖」(帶有行...)並打印它呢?
它可能在網站上?無需下載。
這裏的方式就是我要做的出口......(也許我會得到一些進步^ _)
using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
public class GridViewExportUtil
{
static HSSFWorkbook hssfworkbook;
static MemoryStream WriteToStream()
{
//Write the stream data of workbook to the root directory
MemoryStream file = new MemoryStream();
hssfworkbook.Write(file);
return file;
}
static void InitializeWorkbook()
{
hssfworkbook = new HSSFWorkbook();
////create a entry of DocumentSummaryInformation
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "";
hssfworkbook.DocumentSummaryInformation = dsi;
////create a entry of SummaryInformation
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "";
hssfworkbook.SummaryInformation = si;
}
/// <param name="fileName"></param>
/// <param name="gv"></param>
public static void Export(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Charset = System.Text.Encoding.Unicode.EncodingName;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
//HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; // NPOI
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format(
"attachment; filename=Report.xls"));//, fileName)); // Need .XLS file
HttpContext.Current.Response.Clear();
InitializeWorkbook();
HSSFSheet sheet1 = hssfworkbook.CreateSheet("Таблица");
//sheet1.CreateRow(0).CreateCell(0).SetCellValue("Таблица");
using (StringWriter sw = new StringWriter())
{
// Create a form to contain the grid
Table table = new Table();
// add the header row to the table
if (gv.HeaderRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
}
// add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
GridViewExportUtil.PrepareControlForExport(row);
table.Rows.Add(row);
}
// add the footer row to the table
if (gv.FooterRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
sheet1.DisplayGridlines = true;
HSSFCellStyle style1 = hssfworkbook.CreateCellStyle();
style1.Alignment = HSSFCellStyle.ALIGN_CENTER;
sheet1.SetColumnWidth(0, 10000);
sheet1.SetColumnWidth(1, 5000);
sheet1.VerticallyCenter = true;
for (int j = 2; j < table.Rows[0].Cells.Count; j++)
{
sheet1.SetColumnWidth(j, 4000);
sheet1.SetDefaultColumnStyle(short.Parse(j.ToString()), style1);
}
double Temp=0;
for(int i=0; i<(table.Rows.Count-1); i++)
{
for (int j = 0; j < table.Rows[i].Cells.Count;j++)
{
if (i != 0 && j != 0)
{
if (double.TryParse(table.Rows[i].Cells[j].Text, out Temp))
{
sheet1.CreateRow(i).CreateCell(j).SetCellValue(Temp.ToString());
}
}
else
{
sheet1.CreateRow(i).CreateCell(j).SetCellValue(table.Rows[i].Cells[j].Text);
}
}
}
HttpContext.Current.Response.BinaryWrite(WriteToStream().GetBuffer());
HttpContext.Current.Response.End();
}
}
/// <summary>
/// Replace any of the contained controls with literals
/// </summary>
/// <param name="control"></param>
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
}
if (current.HasControls())
{
GridViewExportUtil.PrepareControlForExport(current);
}
}
}
}
大失敗是我無法打印水平滾動條:S – Cynede 2010-03-16 09:12:09
你是什麼意思水平滾動條? – hallie 2010-03-16 09:18:58
我的意思是我的面板中的網格有水平滾動條:/如果我打印,我只能看到當前的地方 – Cynede 2010-03-16 10:00:46