C#中如何对excel设置A3纸 C#导数据到EXCELExcel.ApplicationClass Mylxls = new Excel.ApplicationClass(); Mylxls.Application.Workbooks.Add(true);想设置打印机,打印纸,打印方向等,怎么写? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //增加一工作薄 WorkbookexlWorkBook = exlApplication.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);//新建工作薄后默认有一个工作表,取得第一个工作表exlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)exlWorkBook.Sheets[1];//设置页面设置exlWorkSheet.PageSetup.出很多设置项 打开Excel的对象浏览器,查看一下有没有对Page 的说明就知道了. .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0.275590551181102) .RightMargin = Application.InchesToPoints(0.275590551181102) .TopMargin = Application.InchesToPoints(0.826771653543307) .BottomMargin = Application.InchesToPoints(0.826771653543307) .HeaderMargin = Application.InchesToPoints(0.511811023622047) .FooterMargin = Application.InchesToPoints(0.511811023622047) .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .CenterHorizontally = False .CenterVertically = False .Orientation = xlLandscape .Draft = False .PaperSize = xlPaperA3 .FirstPageNumber = xlAutomatic .Order = xlDownThenOver .BlackAndWhite = False .Zoom = 100 .PrintErrors = xlPrintErrorsDisplayed 这些操作不是excel的熟悉了吧这些都是打印的选项。纯粹excel也没有这些功能吧,都是打印的时候选择的。这个问题楼主可以考虑要用报表工具比如水晶报表、rdlc都可以,那里面模板可以设置好纸张类型打印方向等 C#设置EXCEL打印纸张表单格式问题 (转)2008-03-27 17:31因为工作上的需要,之前同事做了一个用于打印的Windows Service,后来交由我继续开发维护。 其实就是通过轮询数据库取得数据,然后调用COM进行打印的。但纸张的表单格式需要自定义,这个我在打印机和传真的服务器属性中已经新建好,暂且为它命名为“TEMPForm”。之前同事是通过枚举转换来取得该表单格式并赋值给Excel.Worksheet的PageSetup.PaperSize,代码如下: 之前这个是可以正常使用的,可后来不行了,会抛出异常,提示“TEMPForm”不存在。我比较在意的是之前为什么可以使用,因为枚举中是没有“TEMPForm”的,它是怎么转换成功的? 后来我改用System.Drawing.Printing.PaperSize取得“TEMPForm”的PagerSize的RawKind成员再转换为Excel.XlPaperSize赋值给oSheet.PageSetup.PaperSize,成功!但问题又来了,似乎这个做法有副作用,打印机的纸张来源被改动了——由“拖纸器”变成了“手动进纸”,如此一来影响了客户的正常使用——打印是需要用到拖纸器自动进纸的。((Excel.Worksheet)_xlApp.ActiveWorkbook.ActiveSheet).PageSetup.PaperSize = (Excel.XlPaperSize)rawKind;rawKind为自定义纸张的rawkind 希望能有朋友为我解决以上问题:枚举转换能成功或者使用System.Drawing.Printing.PaperSize而又可以使用拖纸器。 该问题今天我已经处理,在服务启动时就先获取了打印表单格式,在需要的时候再转换并赋值到PagerSize,并在EXCEL模板中做了相应的调整,解决。oSheet.PageSetup.PaperSize = (Excel.XlPaperSize)Enum.Parse(typeof(Excel.XlPaperSize), "TEMPForm"); 先引入组件Interop.Excel.dll,下载地址:http://www.haohao888.com.cn/Soft/HTML/31.htmlprotected void Page_Load(object sender, EventArgs e){ string msg = ""; bool next = true; Excel.Application ObjExcel = new Excel.Application(); Excel._Workbook WB = null; Excel._Worksheet WS = null; Excel.Range range = null; ObjExcel.DisplayAlerts = false; //不弹出保存对话框 try { object miss = System.Reflection.Missing.Value; try { WB = ObjExcel.Workbooks.Open("D:\\t.xls", miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss); } catch { msg += "EXCEL路径不正确!<br>"; next = false; } if (next) { try { WS = (Excel.Worksheet)WB.Worksheets["Sheet1"]; } catch { msg += "工作表名不正确!<br>"; next = false; } if (next) { try { range = WS.get_Range("A1", "B2"); range.ColumnWidth = 50; for (int i = 0; i < 1000; i++) { range = null; string c1 = "A" + ((int)(i + 1)).ToString(); range = WS.get_Range(c1, miss); range.RowHeight = (i % 4 + 1) * 50; } } catch { msg += "版面设置失败!<br>"; next = false; } if (next) { try { WS.PageSetup.HeaderMargin = 0.8; WS.PageSetup.FooterMargin = 0.8; WS.PageSetup.LeftMargin = ObjExcel.InchesToPoints(0.5); WS.PageSetup.RightMargin = ObjExcel.InchesToPoints(0.5); WS.PageSetup.TopMargin = ObjExcel.InchesToPoints(0.5); WS.PageSetup.BottomMargin = ObjExcel.InchesToPoints(0.5); WS.PageSetup.CenterHorizontally = true; WS.PageSetup.CenterVertically = true; } catch { msg += "你没有安装打印机,页边距设置失败!<br>"; } WB.Save(); } } } } catch { msg += "无法打开EXCEL!请检查路径和表名。<br>"; } finally { try { ObjExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(WS); System.Runtime.InteropServices.Marshal.ReleaseComObject(WB); System.Runtime.InteropServices.Marshal.ReleaseComObject(ObjExcel); System.Runtime.InteropServices.Marshal.ReleaseComObject(range); WS = null; WB = null; ObjExcel = null; range = null; GC.Collect(); } catch { msg += "无法结束进程,请手动结束进程!<br>"; } }} C#操作Excel导入导出(2009-05-12 09:21:53)标签:it 分类:C#.NET1.获取Excel的表名string link = FileUpload1.PostedFile.FileName; //FileUpload1为上传控件TB_path.Text = link; //存储路径到TB_path这个控件string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + link + ";" + "Extended Properties=Excel 8.0;";OleDbConnection conn = new OleDbConnection(strConn);conn.Open();DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });DDL_tablename.Items.Clear(); //DDL_tablename下拉菜单DDL_tablename.DataSource = dtSheetName;DDL_tablename.DataTextField = "TABLE_NAME";DDL_tablename.DataValueField = "TABLE_NAME";DDL_tablename.DataBind();2.根据路径和表名获取数据表绑定到DataList控件string link = TB_path.Text.Trim();string tablename = DDL_tablename.SelectedValue.ToString().Trim();string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + link + ";Extended Properties=Excel 8.0";OleDbConnection cnnxls = new OleDbConnection(strConn);OleDbDataAdapter oda = new OleDbDataAdapter("select * from [" + tablename + "]", cnnxls);DataSet ds = new DataSet();oda.Fill(ds);DataTable dt = ds.Table[0];DL_EXCEL.DataSource = dt;DL_EXCEL.DataBind();3.从DataList控件导出到ExcelHttpContext.Current.Response.Charset = "gb2312";HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;HttpContext.Current.Response.ContentType = "application/ms-excel";HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=excelName.xls");DL_EXCEL.Page.EnableViewState = false;System.IO.StringWriter tw = new System.IO.StringWriter();System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);DL_EXCEL.RenderControl(hw);HttpContext.Current.Response.Write(tw.ToString().Replace("<br>", "<br style='mso-data-placement:same-cell;'/>"));HttpContext.Current.Response.End();4.Excel打印页面设置(见我的博客)//解决报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内 public override void VerifyRenderingInServerForm(Control control) { } DataGridViewComboBoxColumn 疑问解答? 紧急求助 安装.NET问题 在线等待 如何实现程序界面切换时在载入数据的时候,有一个类似启动界面中SPLASH一样的东西提示用户程序正在载入 紧急求助!如何像msn和迅雷一样通过快捷方式显示隐藏窗口 求高手帮忙转换下这段javascript代码到c# 如何给mdi主窗口加一个网页背景并且不影响子窗口的显示 在C#中有没有与API函数GetTextExtentPoint32一样功能的函数? 资源文件 在学习C#时一个不明白的地方 请教关于本地数据库缓存LocalDataCache1.sync C#中的函数与方法的区别??
exlWorkBook = exlApplication.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
//新建工作薄后默认有一个工作表,取得第一个工作表
exlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)exlWorkBook.Sheets[1];
//设置页面设置
exlWorkSheet.PageSetup.出很多设置项
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.275590551181102)
.RightMargin = Application.InchesToPoints(0.275590551181102)
.TopMargin = Application.InchesToPoints(0.826771653543307)
.BottomMargin = Application.InchesToPoints(0.826771653543307)
.HeaderMargin = Application.InchesToPoints(0.511811023622047)
.FooterMargin = Application.InchesToPoints(0.511811023622047)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperA3
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 100
.PrintErrors = xlPrintErrorsDisplayed
这些都是打印的选项。纯粹excel也没有这些功能吧,都是打印的时候选择的。
这个问题楼主可以考虑要用报表工具
比如水晶报表、rdlc都可以,
那里面模板可以设置好纸张类型
打印方向等
2008-03-27 17:31
因为工作上的需要,之前同事做了一个用于打印的Windows Service,后来交由我继续开发维护。
其实就是通过轮询数据库取得数据,然后调用COM进行打印的。但纸张的表单格式需要自定义,这个我在打印机和传真的服务器属性中已经新建好,暂且为它命名为“TEMPForm”。之前同事是通过枚举转换来取得该表单格式并赋值给Excel.Worksheet的PageSetup.PaperSize,代码如下:
之前这个是可以正常使用的,可后来不行了,会抛出异常,提示“TEMPForm”不存在。我比较在意的是之前为什么可以使用,因为枚举中是没有“TEMPForm”的,它是怎么转换成功的? 后来我改用System.Drawing.Printing.PaperSize取得“TEMPForm”的PagerSize的RawKind成员再转换为Excel.XlPaperSize赋值给oSheet.PageSetup.PaperSize,成功!但问题又来了,似乎这个做法有副作用,打印机的纸张来源被改动了——由“拖纸器”变成了“手动进纸”,如此一来影响了客户的正常使用——打印是需要用到拖纸器自动进纸的。((Excel.Worksheet)_xlApp.ActiveWorkbook.ActiveSheet).PageSetup.PaperSize = (Excel.XlPaperSize)rawKind;rawKind为自定义纸张的rawkind 希望能有朋友为我解决以上问题:枚举转换能成功或者使用System.Drawing.Printing.PaperSize而又可以使用拖纸器。 该问题今天我已经处理,在服务启动时就先获取了打印表单格式,在需要的时候再转换并赋值到PagerSize,并在EXCEL模板中做了相应的调整,解决。oSheet.PageSetup.PaperSize = (Excel.XlPaperSize)Enum.Parse(typeof(Excel.XlPaperSize), "TEMPForm");
先引入组件Interop.Excel.dll,下载地址:http://www.haohao888.com.cn/Soft/HTML/31.html
protected void Page_Load(object sender, EventArgs e)
{
string msg = "";
bool next = true;
Excel.Application ObjExcel = new Excel.Application();
Excel._Workbook WB = null;
Excel._Worksheet WS = null;
Excel.Range range = null;
ObjExcel.DisplayAlerts = false; //不弹出保存对话框
try
{
object miss = System.Reflection.Missing.Value;
try
{
WB = ObjExcel.Workbooks.Open("D:\\t.xls", miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss);
}
catch
{
msg += "EXCEL路径不正确!<br>";
next = false;
}
if (next)
{
try
{
WS = (Excel.Worksheet)WB.Worksheets["Sheet1"];
}
catch
{
msg += "工作表名不正确!<br>";
next = false;
}
if (next)
{
try
{
range = WS.get_Range("A1", "B2");
range.ColumnWidth = 50;
for (int i = 0; i < 1000; i++)
{
range = null;
string c1 = "A" + ((int)(i + 1)).ToString();
range = WS.get_Range(c1, miss);
range.RowHeight = (i % 4 + 1) * 50;
}
}
catch
{
msg += "版面设置失败!<br>";
next = false;
}
if (next)
{
try
{
WS.PageSetup.HeaderMargin = 0.8;
WS.PageSetup.FooterMargin = 0.8;
WS.PageSetup.LeftMargin = ObjExcel.InchesToPoints(0.5);
WS.PageSetup.RightMargin = ObjExcel.InchesToPoints(0.5);
WS.PageSetup.TopMargin = ObjExcel.InchesToPoints(0.5);
WS.PageSetup.BottomMargin = ObjExcel.InchesToPoints(0.5);
WS.PageSetup.CenterHorizontally = true;
WS.PageSetup.CenterVertically = true;
}
catch
{
msg += "你没有安装打印机,页边距设置失败!<br>";
}
WB.Save();
}
}
}
}
catch
{
msg += "无法打开EXCEL!请检查路径和表名。<br>";
}
finally
{
try
{
ObjExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(WS);
System.Runtime.InteropServices.Marshal.ReleaseComObject(WB);
System.Runtime.InteropServices.Marshal.ReleaseComObject(ObjExcel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
WS = null;
WB = null;
ObjExcel = null;
range = null;
GC.Collect();
}
catch
{
msg += "无法结束进程,请手动结束进程!<br>";
}
}
}
C#操作Excel导入导出(2009-05-12 09:21:53)
标签:it 分类:C#.NET
1.获取Excel的表名string link = FileUpload1.PostedFile.FileName; //FileUpload1为上传控件
TB_path.Text = link; //存储路径到TB_path这个控件string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + link + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
DDL_tablename.Items.Clear(); //DDL_tablename下拉菜单
DDL_tablename.DataSource = dtSheetName;
DDL_tablename.DataTextField = "TABLE_NAME";
DDL_tablename.DataValueField = "TABLE_NAME";
DDL_tablename.DataBind();
2.根据路径和表名获取数据表绑定到DataList控件string link = TB_path.Text.Trim();
string tablename = DDL_tablename.SelectedValue.ToString().Trim();string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + link + ";Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection(strConn);
OleDbDataAdapter oda = new OleDbDataAdapter("select * from [" + tablename + "]", cnnxls);
DataSet ds = new DataSet();
oda.Fill(ds);
DataTable dt = ds.Table[0];
DL_EXCEL.DataSource = dt;
DL_EXCEL.DataBind();
3.从DataList控件导出到ExcelHttpContext.Current.Response.Charset = "gb2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=excelName.xls");
DL_EXCEL.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DL_EXCEL.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString().Replace("<br>", "<br style='mso-data-placement:same-cell;'/>"));
HttpContext.Current.Response.End();
4.Excel打印页面设置(见我的博客)
//解决报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)
{
}