ここで私はボタンクリックで私のcreateReportsメソッドのパラメータとして未知数List<T>
を呼び出そうとしています。私がbutton1
をクリックすると、CustomersInfo
クラスからのリストが必要です。button2
をクリックすると、ExpenseInfo
クラスからのリストを希望します。 これは可能ですか?不明渡しリスト<T>をパラメータとして使用しますか?
コードが更新されました。
class CustomersInfo
{
public string Name
{
get;
set;
}
public string Amount
{
get;
set;
}
}
class ExpenseInfo
{
public string Category
{
get;
set;
}
public string Name
{
get;
set;
}
public string Amount
{
get;
set;
}
}
List<CustomersInfo> customerInfo = new List<CustomersInfo>();
List<ExpenseInfo> expenseInfo = new List<ExpenseInfo>();
private void button1_Click(object sender,EventArgs e)
{
createReports(button1.Text, customerInfo);
}
private void button2_Click(object sender, EventArgs e)
{
createReports(button2.Text, expenseInfo);
}
viewForm.documentViewer1.CloseDocument();
string fileName = Application.StartupPath + "\\**\\report.docx";
if (File.Exists(fileName))
{
File.Delete(fileName);
}
try
{
var application = new Microsoft.Office.Interop.Word.Application();
var distinct = report.Distinct().GroupBy(x => x.Name).Select(y => y.First());
var document = application.Documents.Add(Template: Application.StartupPath + "/**/Templates/Reports.docx");
total = 0;
foreach (Microsoft.Office.Interop.Word.Field field in document.Fields)
{
if (field.Code.Text.Contains("Info"))
{
field.Select();
application.Selection.TypeText(Label);
}
else if (field.Code.Text.Contains("Grid"))
{
field.Select();
int RowCount = distinct.Count();
int ColumnCount = type.GetProperties().Length;
Object[,] DataArray = new object[RowCount, ColumnCount + 1];
//add rows
int r = 0;
int d = 0;
foreach (var client in distinct)
{
clientTotal = 0;
foreach (var info in report)
{
if (client.Name == info.Name)
{
clientTotal = Convert.ToDecimal(info.Amount.Remove(0, 1)) + clientTotal;
}
}
total = total + clientTotal;
DataArray[r, 0] = client.Name;
DataArray[r++, 1] = clientTotal.ToString("C2");
}
//page orintation
document.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;
dynamic oRange = document.Content.Application.Selection.Range;
string oTemp = "";
for (r = 0; r <= RowCount - 1; r++)
{
for (int c = 0; c <= ColumnCount - 1; c++)
{
oTemp = oTemp + DataArray[r, c] + "\t";
}
}
//table format
oRange.Text = oTemp;
object Separator = Microsoft.Office.Interop.Word.WdTableFieldSeparator.wdSeparateByTabs;
object ApplyBorders = true;
object AutoFit = true;
object AutoFitBehavior = Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitFixed;
oRange.ConvertToTable(ref Separator, ref RowCount, ref ColumnCount,
Type.Missing, Type.Missing, ref ApplyBorders,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, ref AutoFit, ref AutoFitBehavior, Type.Missing);
oRange.Select();
document.Application.Selection.Tables[1].Select();
document.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0;
document.Application.Selection.Tables[1].Rows.Alignment = 0;
document.Application.Selection.Tables[1].Rows[1].Select();
document.Application.Selection.InsertRowsAbove(1);
document.Application.Selection.Tables[1].Rows[1].Select();
//header row style
document.Application.Selection.Tables[1].Rows[1].Range.Bold = 1;
document.Application.Selection.Tables[1].Rows[1].Range.Font.Name = "Arial";
document.Application.Selection.Tables[1].Rows[1].Range.Font.Size = 11;
//add header row manually
document.Application.Selection.Tables[1].Cell(1, 1).Range.Text = "Client";
document.Application.Selection.Tables[1].Cell(1, 2).Range.Text = "Amount";
//table style
document.Application.Selection.Tables[1].set_Style("Grid Table 4 - Accent 1");
for (int c = 1; c <= document.Application.Selection.Tables[1].Rows.Count - 1; c++)
{
document.Application.Selection.Tables[1].Rows[c].Range.Font.Size = 9;
}
document.Application.Selection.Tables[1].Rows[1].Select();
document.Application.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
}
else if (field.Code.Text.Contains("total"))
{
field.Select();
application.Selection.TypeText("Total Income by Client " + total.ToString("C2"));
}
}
document.SaveAs(fileName);
document.Close();
application.Quit();
application = null;
document = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
catch (Exception ex)
{
// Get stack trace for the exception with source file information
var st = new StackTrace(ex, true);
// Get the top stack frame
var frame = st.GetFrame(st.FrameCount - 1);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();
MessageBox.Show(line.ToString());
}
showReports(Label);
ご協力いただければ幸いです。前もって感謝します。
理想的には、共通の基本タイプを使用します。どのようなものが実際には何かを知ることは興味深いでしょう...型付きのリストを持たずに、どのようにリストアイテムのメンバーとやりとりするのですか? –
病気を加える。 –