我一直在尝试使用C#将ListView控件的内容数据导出到Excel应用程序中。在使用VB.NET之前我已经完成了这个,我尝试将我的代码转换为C#但是它失败但是我对它进行了修改,看起来下面的代码一切都很好,除了最后一部分将数据保存为excel文件。我需要帮助才能正确修改此代码。非常感谢您的回复。
下面出现错误的代码是:
oBook.SaveAs(SaveFileDialog1.InitialDirectory.ToString() + SaveFileDialog1.FileName);
错误5方法'SaveAs'没有重载需要'1'参数
private void Button4_Click(object sender, System.EventArgs e)
{
int row = 0;
int col = 0;
int row2 = 0;
int col2 = 0;
int ch = 0;
int ctr = 0;
ctr = 0;
row2 = 1;
col2 = 3;
row = 3;
col = 3;
Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application();
oExcel.Visible = false;
Microsoft.Office.Interop.Excel.Workbook oBook = oExcel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
Microsoft.Office.Interop.Excel.Worksheet oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oExcel.ActiveSheet;
SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
SaveFileDialog1.Filter = "Excel File|*.xlsx|Word File|*.doc|Text File|*.txt";
SaveFileDialog1.Title = "Save As";
if (SaveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
switch (SaveFileDialog1.FilterIndex)
{
case 1:
{
for (ch = 1; ch <= ListView1.Columns.Count; ch++)
{
oSheet.Cells[row2, col2] = ListView1.Columns[ctr].Text;
col2 = col2 + 1;
ctr = ctr + 1;
}
foreach (ListViewItem lview in ListView1.Items)
{
foreach (ListViewItem.ListViewSubItem lview2 in lview.SubItems)
{
oSheet.Cells[row, col] = lview2.Text;
col = col + 1;
}
col = 3;
row = row + 1;
}
oBook.SaveAs(SaveFileDialog1.InitialDirectory.ToString() + SaveFileDialog1.FileName);
oExcel.Quit();
SaveFileDialog1.Dispose();
MessageBox.Show("Data has been successfully saved", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
}
答案 0 :(得分:0)
工作簿SaveAs方法需要11个参数。在C#中,即使您不关心它们,也必须为这些可选参数传递一些内容。最简单的方法是为那些你不关心的人传递System.Reflection.Missing.Value
。但这很痛苦。