C# 如何使Autofill for excel正常工作?
更新:我尝试过使用其他Excel.XlAutoFillType,可惜它不起作用 类型为() 原始问题:C# 如何使Autofill for excel正常工作?,c#,excel,vsto,C#,Excel,Vsto,更新:我尝试过使用其他Excel.XlAutoFillType,可惜它不起作用 类型为() 原始问题: // Datetime dates[] = new dates[3] {dt1, dt2, dt3} // Int[] dateLength = new Int[3] {0, (int)Math.Ceiling(dates[1].Subtract(dates[0]).TotalDays), ((int)Math.Ceiling(dates[2].Subtract(dates[1]).Total
// Datetime dates[] = new dates[3] {dt1, dt2, dt3}
// Int[] dateLength = new Int[3] {0, (int)Math.Ceiling(dates[1].Subtract(dates[0]).TotalDays), ((int)Math.Ceiling(dates[2].Subtract(dates[1]).TotalDays) + (int)Math.Ceiling(dates[1].Subtract(dates[0]).TotalDays))}
for (int i = 0, j = 0; i < dates.Length; i++, j++)
{
Excel.Worksheet activeWst = Globals.ThisAddIn.Application.ActiveSheet;
Excel.Range tRange = activeWst.Range[activeWst.Cells[dateLength[i] + 2 + j, 2], activeWst.Cells[dateLength[i] + 2 + j, 2]];
tRange.Value = dates[i].ToString("dd/MM/yyyy");
if (i < dates.Length - 1) {
tRange.AutoFill(activeWst.Range[activeWst.Cells[dateLength[i] + 2 + j, 2], activeWst.Cells[dateLength[i + 1] + j + 1, 2]]);
}
}
我是VSTO新手,现在正在为Excel(2013或更高版本)编写VSTO加载项。自动填充方法是Microsoft.Office.Interop.Excel中的Range.Autofill(Range,XlAutoFillType)(请参阅)
我有一个日期(如2020年2月25日),希望excel自动在列的其余部分填入日期(例如A1:25.02.2020;A2:26.02.2020;A3:27.02.2020;…A6:01.03.2020;…)
当我尝试在excel中手动执行时,只要我选择两个或多个单元格作为示例,它就会起作用
图片请参见:(因为StackOverflow不允许我发布照片)
然而,当我尝试使用自动填充方法时,它失败了,只增加了年份。(B2作为excel的示例提供)
图片见:
这是我的代码:
// Datetime dates[] = new dates[3] {dt1, dt2, dt3}
// Int[] dateLength = new Int[3] {0, (int)Math.Ceiling(dates[1].Subtract(dates[0]).TotalDays), ((int)Math.Ceiling(dates[2].Subtract(dates[1]).TotalDays) + (int)Math.Ceiling(dates[1].Subtract(dates[0]).TotalDays))}
for (int i = 0, j = 0; i < dates.Length; i++, j++)
{
Excel.Worksheet activeWst = Globals.ThisAddIn.Application.ActiveSheet;
Excel.Range tRange = activeWst.Range[activeWst.Cells[dateLength[i] + 2 + j, 2], activeWst.Cells[dateLength[i] + 2 + j, 2]];
tRange.Value = dates[i].ToString("dd/MM/yyyy");
if (i < dates.Length - 1) {
tRange.AutoFill(activeWst.Range[activeWst.Cells[dateLength[i] + 2 + j, 2], activeWst.Cells[dateLength[i + 1] + j + 1, 2]]);
}
}
//Datetime dates[]=新日期[3]{dt1,dt2,dt3}
//Int[]dateLength=new Int[3]{0,(Int)Math.天花(日期[1]。减法(日期[0]).TotalDays),((Int)Math.天花(日期[2]。减法(日期[1]).TotalDays)+(Int)Math.天花(日期[1]。减法(日期[0]).TotalDays))}
for(inti=0,j=0;i
谢谢。我自己找到了答案。由于每个单元格中的日期值都存储为字符串,因此自动填充将不起作用
tRange.Value = dates[i].ToString("dd/MM/yyyy"); //The problem
唯一的方法是直接在单元格中写入日期,并将整个列的数字格式更改为您喜欢的格式
//Fixed code
tRange.EntireColumn.NumberFormat = "dd-MM-yyyy";
tRange.Value = dates[i].Date;
顺便说一句,它使用默认的XlAutoFillType(Excel.XlAutoFillType.xlFillDefault)
我感谢大家的帮助。多谢各位 请仔细阅读这篇文章,看看必要的论点。你错过了背后的论点。正如我所提到的,这是没有必要的,因为它将自动设置为xlFillDefault。另外,我不知道该选哪一个,因为我找不到关于短格式日期的任何信息。
namedRange1.AutoFill(this.Range[…],Excel.XlAutoFillType.xlFillDays)代码>您已经尝试过了吗?您的代码:tRange.AutoFill(activeWst.Range[activeWst.Cells[…],activeWst.Cells[…],?)代码>我看不到您的最终参数公共对象自动填充(Microsoft.Office.Interop.Excel.Range目标,Microsoft.Office.Interop.Excel.XlAutoFillType=Microsoft.Office.Interop.Excel.XlAutoFillType.xlFillDefault);所以在我的代码中,它是默认的