日期:2014-05-18  浏览次数:21380 次

十万火急!!!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();
// 将用户所有设置都保存起来