Rya*_*anS 5 c# excel reportviewer datasource visual-studio
我试图使用reportviewerVisual Studio提供的(Windows窗体应用程序)来创建基于Excel电子表格的报告.但是,我很难找到正确的方式来阅读/访问电子表格.
当我尝试创建新报告时,我会看到以下窗口:

我试过使用对象选项,但没有运气
问题:如何使用Excel电子表格创建报告?
我有幸运行以下代码,允许我处理文件,但我找不到将其绑定到reportviewer的方法:
Excel.Application ExcelObj = new Excel.Application();
this.openFileDialog1.FileName = "*.xls";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(
openFileDialog1.FileName, 0, true, 5,
"", "", true, Excel.XlPlatform.xlWindows, "\t", false, false,
0, true);
Excel.Sheets sheets = theWorkbook.Worksheets;
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
for (int i = 1; i <= 10; i++)
{
Excel.Range range = worksheet.get_Range("A" + i.ToString(), "J" + i.ToString());
System.Array myvalues = (System.Array)range.Cells.Value;
string[] strArray = ConvertToStringArray(myvalues);
}
}
Run Code Online (Sandbox Code Playgroud)
欢迎任何建议/指导
经过大量谷歌搜索后,我设法将其拼凑在一起,并将excel表格放入基本报告中.
这要求您设置具有在Excel电子表格的第一行中指定的列名称的数据集,并且还要将此数据集绑定到报表中.然后您可以使用以下内容填充它:
[open file dialog code..]
try
{
string path = this.openFileDialog1.FileName;
if (Path.GetExtension(path) == ".xls")
{
oledbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
}
else if (Path.GetExtension(path) == ".xlsx")
{
oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
}
oledbConn.Open();
DataSet ds = new DataSet();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = oledbConn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM [sheet1$]";
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda = new OleDbDataAdapter(cmd);
oleda.Fill(ds);
ds.Tables[0].TableName = "DataTable1";
this.DataTable1BindingSource.DataSource = ds;
this.reportViewer1.RefreshReport();
}
catch (Exception ex)
{
}
finally
{
oledbConn.Close();
}
Run Code Online (Sandbox Code Playgroud)
我发现这些文章很有帮助:
| 归档时间: |
|
| 查看次数: |
6305 次 |
| 最近记录: |