十万火急!!!C#如何控制EXCEL自定义纸张的大小 如题! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// 将指定范围区域拷贝到目标区域 /// </summary> /// <param name="sheetIndex">WorkSheet索引</param> /// <param name="startCell">要拷贝区域的开始Cell位置(比如:A10)</param> /// <param name="endCell">要拷贝区域的结束Cell位置(比如:F20)</param> /// <param name="targetCell">目标区域的开始Cell位置(比如:H10)</param> public void RangeCopy(int sheetIndex,string startCell,string endCell,string targetCell) { if(sheetIndex > this.WorkSheetCount) { this.KillExcelProcess(); throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!"); } try { workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex); range1 = workSheet.get_Range(startCell,endCell); range2 = workSheet.get_Range(targetCell,this.missing); range1.Copy(range2); } catch(Exception e) { this.KillExcelProcess(); throw e; } } /// <summary> /// 将指定范围区域拷贝到目标区域 /// </summary> /// <param name="sheetName">WorkSheet名称</param> /// <param name="startCell">要拷贝区域的开始Cell位置(比如:A10)</param> /// <param name="endCell">要拷贝区域的结束Cell位置(比如:F20)</param> /// <param name="targetCell">目标区域的开始Cell位置(比如:H10)</param> public void RangeCopy(string sheetName,string startCell,string endCell,string targetCell) { try { Excel.Worksheet sheet = null; for(int i=1;i<=this.WorkSheetCount;i++) { workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i); if(workSheet.Name == sheetName) { sheet = workSheet; } } if(sheet != null) { for(int i=sheetCount;i>=1;i--) { range1 = sheet.get_Range(startCell,endCell); range2 = sheet.get_Range(targetCell,this.missing); range1.Copy(range2); } } else { this.KillExcelProcess(); throw new Exception("名称为\"" + sheetName + "\"的工作表不存在"); } } catch(Exception e) { this.KillExcelProcess(); throw e; } } 打开你的EXCEL新建宏,然后录制新宏,设置自定义纸张大小,然后停止录制,最后 excel 中 "工具"->"宏"->"visual basic "查看具体代码怎么写的,然后就好办了. 我之前搞过这样一个东西 C#可以调用 EXCEL里任何一个菜单的窗口,比如打印机设置窗口,同时还可以将这个窗口的配置值读取出来,我建议你可以看看纸张设置窗口传递出的几个参数,读取他们塞到一个数组里,每次要设置的时候旧调用他 public Hashtable getPrinterSet() { Word.Application privewapp = null; Word.DocumentClass privewdoc = null; printset = new Hashtable(); //第一次运行打印的时候弹出WORD打印对话框 并保存用户设置 object hai = 0; privewapp = new Word.ApplicationClass(); object templateFile = Application.StartupPath + @"\data\salary.dot"; privewapp.Visible = false; privewdoc = (Word.DocumentClass)privewapp.Documents.Add(ref templateFile, ref missing, ref missing, ref missing); privewdoc.Activate(); Word.Dialog dlg = privewapp.Dialogs[Word.WdWordDialog.wdDialogFilePrint]; dlg.Display(ref hai); //dlg.Execute(); // 将用户所有设置都保存起来 Object printer = vb.Interaction.CallByName(dlg, "printer", vb.CallType.Get); Object To = vb.Interaction.CallByName(dlg, "To", vb.CallType.Get); Object Background = vb.Interaction.CallByName(dlg, "Background", vb.CallType.Get); Object AppendPrFile = vb.Interaction.CallByName(dlg, "AppendPrFile", vb.CallType.Get); Object Range = vb.Interaction.CallByName(dlg, "Range", vb.CallType.Get); Object PrToFileName = vb.Interaction.CallByName(dlg, "PrToFileName", vb.CallType.Get); Object From = vb.Interaction.CallByName(dlg, "From", vb.CallType.Get); Object Type = vb.Interaction.CallByName(dlg, "Type", vb.CallType.Get); Object NumCopies = vb.Interaction.CallByName(dlg, "NumCopies", vb.CallType.Get); Object Pages = vb.Interaction.CallByName(dlg, "Pages", vb.CallType.Get); Object Order = vb.Interaction.CallByName(dlg, "Order", vb.CallType.Get); Object PrintToFile = vb.Interaction.CallByName(dlg, "PrintToFile", vb.CallType.Get); Object Collate = vb.Interaction.CallByName(dlg, "Collate", vb.CallType.Get); Object FileName = vb.Interaction.CallByName(dlg, "FileName", vb.CallType.Get); Object OutputPrinter = vb.Interaction.CallByName(dlg, "OutputPrinter", vb.CallType.Get); Object DuplexPrint = vb.Interaction.CallByName(dlg, "DuplexPrint", vb.CallType.Get); Object PrintZoomColumn = vb.Interaction.CallByName(dlg, "PrintZoomColumn", vb.CallType.Get); Object PrintZoomRow = vb.Interaction.CallByName(dlg, "PrintZoomRow", vb.CallType.Get); Object PrintZoomPaperHeight = vb.Interaction.CallByName(dlg, "PrintZoomPaperHeight", vb.CallType.Get); Object ZoomPaper = vb.Interaction.CallByName(dlg, "ZoomPaper", vb.CallType.Get); printset.Add("printer", printer); printset.Add("To", To); printset.Add("Background", Background); printset.Add("AppendPrFile", AppendPrFile); printset.Add("Range", Range); printset.Add("PrToFileName", PrToFileName); printset.Add("From", From); printset.Add("Type", Type); printset.Add("NumCopies", NumCopies); printset.Add("Pages", Pages); printset.Add("Order", Order); printset.Add("PrintToFile", PrintToFile); printset.Add("Collate", Collate); printset.Add("FileName", FileName); printset.Add("OutputPrinter", OutputPrinter); printset.Add("DuplexPrint", DuplexPrint); printset.Add("PrintZoomColumn", PrintZoomColumn); printset.Add("PrintZoomRow", PrintZoomRow); printset.Add("PrintZoomPaperHeight", PrintZoomPaperHeight); printset.Add("ZoomPaper", ZoomPaper); return printset; }我这个是调用WORD的打印窗口,设置打印参数并保存起来,当连续打印的时候 每次都按照这个设置来.你可以参考下,EXCEL和WORD 的调用方法很相近! 自己搞定!!!///使用方法 PageSetup.PaperSize = (Excel.XlPaperSize)getpname("自定义纸张名称"); protected int getpname(string pname) { int aaa = 9;//默认A4 System.Drawing.Printing.PrinterSettings printer = new System.Drawing.Printing.PrinterSettings(); printer.PrinterName = "要用的打印机名称"; foreach (System.Drawing.Printing.PaperSize ps in printer.PaperSizes) { if (ps.PaperName.Trim() == pname) { aaa = ps.RawKind; } } return aaa; } 设计一个CS结构的连锁店的软件你会怎么做 我收到外部设备的这些信息,应该如何转化为相应的数据呢?C# 怎么让异步调用停止 怎样彻底删除.aspx和.aspx.cs文件? 在DataGrid的同一列中可以加如两种以上不同的控件吗 急救 !!!!!!!!水晶报表 问题!!!!!!! 页面的锁定 IIS 寄宿 wcf 问题。数据清空了。 这儿人气旺,我是否应该从VB。NET跑到C#来? 我多了一个三角星了,散分,快来啊,不过最多还是只有100分,大家不要嫌少啊 ListView 控件 后台登陆权限的问题
/// </summary>
/// <param name="sheetIndex">WorkSheet索引</param>
/// <param name="startCell">要拷贝区域的开始Cell位置(比如:A10)</param>
/// <param name="endCell">要拷贝区域的结束Cell位置(比如:F20)</param>
/// <param name="targetCell">目标区域的开始Cell位置(比如:H10)</param>
public void RangeCopy(int sheetIndex,string startCell,string endCell,string targetCell)
{
if(sheetIndex > this.WorkSheetCount)
{
this.KillExcelProcess();
throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!");
} try
{
workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex);
range1 = workSheet.get_Range(startCell,endCell);
range2 = workSheet.get_Range(targetCell,this.missing); range1.Copy(range2);
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
} /// <summary>
/// 将指定范围区域拷贝到目标区域
/// </summary>
/// <param name="sheetName">WorkSheet名称</param>
/// <param name="startCell">要拷贝区域的开始Cell位置(比如:A10)</param>
/// <param name="endCell">要拷贝区域的结束Cell位置(比如:F20)</param>
/// <param name="targetCell">目标区域的开始Cell位置(比如:H10)</param>
public void RangeCopy(string sheetName,string startCell,string endCell,string targetCell)
{
try
{
Excel.Worksheet sheet = null; for(int i=1;i<=this.WorkSheetCount;i++)
{
workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(i);
if(workSheet.Name == sheetName)
{
sheet = workSheet;
}
} if(sheet != null)
{
for(int i=sheetCount;i>=1;i--)
{
range1 = sheet.get_Range(startCell,endCell);
range2 = sheet.get_Range(targetCell,this.missing); range1.Copy(range2);
}
}
else
{
this.KillExcelProcess();
throw new Exception("名称为\"" + sheetName + "\"的工作表不存在");
}
}
catch(Exception e)
{
this.KillExcelProcess();
throw e;
}
}
新建宏,然后录制新宏,设置自定义纸张大小,然后停止录制,最后 excel 中 "工具"->"宏"->"visual basic "查看
具体代码怎么写的,然后就好办了.
C#可以调用 EXCEL里任何一个菜单的窗口,比如打印机设置窗口,同时还可以将这个窗口的配置值读取出来,
我建议你可以看看纸张设置窗口传递出的几个参数,读取他们塞到一个数组里,每次要设置的时候旧调用他
{
Word.Application privewapp = null;
Word.DocumentClass privewdoc = null;
printset = new Hashtable();
//第一次运行打印的时候弹出WORD打印对话框 并保存用户设置
object hai = 0;
privewapp = new Word.ApplicationClass();
object templateFile = Application.StartupPath + @"\data\salary.dot";
privewapp.Visible = false;
privewdoc = (Word.DocumentClass)privewapp.Documents.Add(ref templateFile, ref missing, ref missing, ref missing);
privewdoc.Activate();
Word.Dialog dlg = privewapp.Dialogs[Word.WdWordDialog.wdDialogFilePrint];
dlg.Display(ref hai);
//dlg.Execute();
// 将用户所有设置都保存起来
Object printer = vb.Interaction.CallByName(dlg, "printer", vb.CallType.Get);
Object To = vb.Interaction.CallByName(dlg, "To", vb.CallType.Get);
Object Background = vb.Interaction.CallByName(dlg, "Background", vb.CallType.Get);
Object AppendPrFile = vb.Interaction.CallByName(dlg, "AppendPrFile", vb.CallType.Get);
Object Range = vb.Interaction.CallByName(dlg, "Range", vb.CallType.Get);
Object PrToFileName = vb.Interaction.CallByName(dlg, "PrToFileName", vb.CallType.Get);
Object From = vb.Interaction.CallByName(dlg, "From", vb.CallType.Get);
Object Type = vb.Interaction.CallByName(dlg, "Type", vb.CallType.Get);
Object NumCopies = vb.Interaction.CallByName(dlg, "NumCopies", vb.CallType.Get);
Object Pages = vb.Interaction.CallByName(dlg, "Pages", vb.CallType.Get);
Object Order = vb.Interaction.CallByName(dlg, "Order", vb.CallType.Get);
Object PrintToFile = vb.Interaction.CallByName(dlg, "PrintToFile", vb.CallType.Get);
Object Collate = vb.Interaction.CallByName(dlg, "Collate", vb.CallType.Get);
Object FileName = vb.Interaction.CallByName(dlg, "FileName", vb.CallType.Get);
Object OutputPrinter = vb.Interaction.CallByName(dlg, "OutputPrinter", vb.CallType.Get);
Object DuplexPrint = vb.Interaction.CallByName(dlg, "DuplexPrint", vb.CallType.Get);
Object PrintZoomColumn = vb.Interaction.CallByName(dlg, "PrintZoomColumn", vb.CallType.Get);
Object PrintZoomRow = vb.Interaction.CallByName(dlg, "PrintZoomRow", vb.CallType.Get);
Object PrintZoomPaperHeight = vb.Interaction.CallByName(dlg, "PrintZoomPaperHeight", vb.CallType.Get);
Object ZoomPaper = vb.Interaction.CallByName(dlg, "ZoomPaper", vb.CallType.Get); printset.Add("printer", printer);
printset.Add("To", To);
printset.Add("Background", Background);
printset.Add("AppendPrFile", AppendPrFile);
printset.Add("Range", Range);
printset.Add("PrToFileName", PrToFileName);
printset.Add("From", From);
printset.Add("Type", Type);
printset.Add("NumCopies", NumCopies);
printset.Add("Pages", Pages);
printset.Add("Order", Order);
printset.Add("PrintToFile", PrintToFile);
printset.Add("Collate", Collate);
printset.Add("FileName", FileName);
printset.Add("OutputPrinter", OutputPrinter);
printset.Add("DuplexPrint", DuplexPrint);
printset.Add("PrintZoomColumn", PrintZoomColumn);
printset.Add("PrintZoomRow", PrintZoomRow);
printset.Add("PrintZoomPaperHeight", PrintZoomPaperHeight);
printset.Add("ZoomPaper", ZoomPaper); return printset; }
我这个是调用WORD的打印窗口,设置打印参数并保存起来,当连续打印的时候 每次都按照这个设置来.你可以参考下,EXCEL和WORD 的调用方法很相近!
///使用方法 PageSetup.PaperSize = (Excel.XlPaperSize)getpname("自定义纸张名称");
protected int getpname(string pname)
{
int aaa = 9;//默认A4
System.Drawing.Printing.PrinterSettings printer = new System.Drawing.Printing.PrinterSettings();
printer.PrinterName = "要用的打印机名称";
foreach (System.Drawing.Printing.PaperSize ps in printer.PaperSizes)
{
if (ps.PaperName.Trim() == pname)
{
aaa = ps.RawKind;
}
}
return aaa;
}