在asp.net中怎么样添加保存和打印功能?
展开全部
这个是execl的生成方法
public static string CreateExcel(DataTable dt)
{
string dateName = DateTime.Now.ToString("yyyy-MM-dd HH/mm/ss");
string fileNameString = GetFilePath() + "/重庆长安加液设备 - SAT 加注及检测打印报告" + dateName + ".xls";//excel存放地址,文件名
string strLine = "";
FileStream objFileStream = new FileStream(fileNameString, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
for (int i = 0; i < dt.Rows.Count; i++)
{
strLine = "重庆长安加液设备 - SAT 加注及检测打印报告";
//objStreamWriter.WriteLine(strLine);换一行书写
objStreamWriter.WriteLine(strLine);
//Convert.ToChar(9) 相当于换一格写,也就是按了下Tab键
strLine = "车辆VIN号:" + Convert.ToChar(9) + dt.Rows[i]["CarVIN"].ToString() + Convert.ToChar(9) + " 车辆类型:" + Convert.ToChar(9) + dt.Rows[i]["CarType"].ToString() + Convert.ToChar(9) + " 车辆名字:" + Convert.ToChar(9) + dt.Rows[i]["CarName"].ToString() + Convert.ToChar(9) + Convert.ToChar(9);
objStreamWriter.WriteLine(strLine);
}
objStreamWriter.Close();
objFileStream.Close();
return fileNameString;
}
这个是pdf的生成方法
protected void But_Excel_Click(object sender, EventArgs e)
{
DataSet ds = (DataSet)Session["CarInfo"];
DataTable dt = new DataTable();
dt = ds.Tables[0];
CreatePDF(dt);
}
public void CreatePDF(DataSet ds)
{
//为将要被创建的Pdf文档指定大小和颜色
iTextSharp.text.Rectangle rec = new Rectangle(PageSize.A4.Rotate());//A4纸横放
//rec.BackgroundColor = new Color(System.Drawing.Color.Plum);
//创建Pdf文档
iTextSharp.text.Document doc = new iTextSharp.text.Document(rec);
//为Document创建多个PdfWriter对象
iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new System.IO.FileStream("D:/chap0101.pdf", System.IO.FileMode.Create));
//可以在Open()方法调用前为doc添加摘要
doc.Open();
//添加一个表格
iTextSharp.text.Table table = new iTextSharp.text.Table(3);
table.BorderWidth = 1;
table.BorderColor = new Color(0, 0, 255);
table.Padding = 5;
table.Spacing = 5;
Cell cell = new Cell("header");
cell.Header = true;
cell.Colspan = 3; //合并单元格
table.AddCell(cell); //添加到表格
cell = new Cell("example cell with colspan 1 and rowspan 2");
//第一个单元格,合并两行
cell.Rowspan = 2;
cell.BorderColor = new Color(255, 0, 0);
table.AddCell(cell);
table.AddCell("1.1");
table.AddCell("2.1");
table.AddCell("1.2");
table.AddCell("2.2");
table.AddCell("cell test1");
cell = new Cell("big cell");
cell.Rowspan = 2;
cell.Colspan = 2;
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
table.AddCell(cell);
table.AddCell("cell test2");
doc.Add(table);
doc.Close();
}
我不知道你需要打印什么文件
public static string CreateExcel(DataTable dt)
{
string dateName = DateTime.Now.ToString("yyyy-MM-dd HH/mm/ss");
string fileNameString = GetFilePath() + "/重庆长安加液设备 - SAT 加注及检测打印报告" + dateName + ".xls";//excel存放地址,文件名
string strLine = "";
FileStream objFileStream = new FileStream(fileNameString, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
for (int i = 0; i < dt.Rows.Count; i++)
{
strLine = "重庆长安加液设备 - SAT 加注及检测打印报告";
//objStreamWriter.WriteLine(strLine);换一行书写
objStreamWriter.WriteLine(strLine);
//Convert.ToChar(9) 相当于换一格写,也就是按了下Tab键
strLine = "车辆VIN号:" + Convert.ToChar(9) + dt.Rows[i]["CarVIN"].ToString() + Convert.ToChar(9) + " 车辆类型:" + Convert.ToChar(9) + dt.Rows[i]["CarType"].ToString() + Convert.ToChar(9) + " 车辆名字:" + Convert.ToChar(9) + dt.Rows[i]["CarName"].ToString() + Convert.ToChar(9) + Convert.ToChar(9);
objStreamWriter.WriteLine(strLine);
}
objStreamWriter.Close();
objFileStream.Close();
return fileNameString;
}
这个是pdf的生成方法
protected void But_Excel_Click(object sender, EventArgs e)
{
DataSet ds = (DataSet)Session["CarInfo"];
DataTable dt = new DataTable();
dt = ds.Tables[0];
CreatePDF(dt);
}
public void CreatePDF(DataSet ds)
{
//为将要被创建的Pdf文档指定大小和颜色
iTextSharp.text.Rectangle rec = new Rectangle(PageSize.A4.Rotate());//A4纸横放
//rec.BackgroundColor = new Color(System.Drawing.Color.Plum);
//创建Pdf文档
iTextSharp.text.Document doc = new iTextSharp.text.Document(rec);
//为Document创建多个PdfWriter对象
iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new System.IO.FileStream("D:/chap0101.pdf", System.IO.FileMode.Create));
//可以在Open()方法调用前为doc添加摘要
doc.Open();
//添加一个表格
iTextSharp.text.Table table = new iTextSharp.text.Table(3);
table.BorderWidth = 1;
table.BorderColor = new Color(0, 0, 255);
table.Padding = 5;
table.Spacing = 5;
Cell cell = new Cell("header");
cell.Header = true;
cell.Colspan = 3; //合并单元格
table.AddCell(cell); //添加到表格
cell = new Cell("example cell with colspan 1 and rowspan 2");
//第一个单元格,合并两行
cell.Rowspan = 2;
cell.BorderColor = new Color(255, 0, 0);
table.AddCell(cell);
table.AddCell("1.1");
table.AddCell("2.1");
table.AddCell("1.2");
table.AddCell("2.2");
table.AddCell("cell test1");
cell = new Cell("big cell");
cell.Rowspan = 2;
cell.Colspan = 2;
cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
table.AddCell(cell);
table.AddCell("cell test2");
doc.Add(table);
doc.Close();
}
我不知道你需要打印什么文件
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询