十万火急!!!C#如何控制EXCEL自定义纸张的大小
如题!
[解决办法]
/// 将指定范围区域拷贝到目标区域
/// </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 的调用方法很相近!