我已经编写了这个教程,下面是一个youtube教程,该教程中的DataSource是一个AOD.NET实体数据模型,我使用的是Access数据库。我编译的代码,但我得到的各种错误,如C#DataGridView到Excel文件错误
The name 'productBindingSource' does not exist in the current context
或
The name 'DB' does not exist in the current context
或
The type or namespace name 'Product' could not be found
我不知道如果我错过了增加一个参考或者如果这些错误是由于数据源不同?
Visual Studio自动添加了//TODO: This line of code etc...
,我将其更改为教程中的显示方式。
我希望有人能帮助告诉我我做错了什么?
教程:https://www.youtube.com/watch?v=-wGzK1vsqS8
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ExportWebsiteData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
List<Product> list = ((DataParameter)e.Argument).ProductList;
string filename = ((DataParameter)e.Argument).FileName;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Workbook wb = excel.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)excel.ActiveSheet;
excel.Visible = false;
int index = 1;
int process = list.Count;
//Add Column
ws.Cells[1, 1] = "Item Number";
ws.cells[1, 2] = "Model";
ws.cells[1, 3] = "Manufacturer";
ws.cells[1, 4] = "Category";
ws.cells[1, 5] = "Subcategory";
//
foreach(Product p in list)
{
if (!backgroundWorker.CancellationPending)
{
backgroundWorker.ReportProgress(index++ * 100/process);
ws.Cells[index, 1] = p.ItemNumber.ToString();
ws.Cells[index, 2] = p.Model.ToString();
ws.Cells[index, 3] = p.Manufacturer.ToString();
ws.Cells[index, 4] = p.Category.ToString();
ws.Cells[index, 5] = p.SubCategory.ToString();
}
}
//Save file
ws.SaveAs(filename, XlFileFormat.xlWorkbookdefault, Type.Missing, Type.Missing, true, false, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
excel.Quit();
}
struct DataParameter
{
public List<Product> ProductList;
public string FileName { get; set; }
}
DataParameter _inputParameter;
private void Form1_Load(object sender, EventArgs e)
{
using (this.inventoryTableAdapter.Fill(this._Wizard_Data_2016_10_17DataSet.Inventory); = new _Wizard_Data_2016_10_17DataSet())
{
productBindingSource.DataSource = DB.Products.ToList();
}
// TODO: This line of code loads data into the '_Wizard_Data_2016_10_17DataSet.Inventory' table. You can move, or remove it, as needed.
//this.inventoryTableAdapter.Fill(this._Wizard_Data_2016_10_17DataSet.Inventory);
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
lblStatus.Text = string.Format("Processing...{0}", e.ProgressPercentage);
progressBar.Update();
}
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if(e.Error == null)
{
Thread.Sleep(100);
lblStatus.Text = "Your data has been successfully exported.";
}
}
private void btnExport_Click(object sender, EventArgs e)
{
if (backgroundWorker.IsBusy)
return;
using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Excel Workbook|*.xls" })
{
if (sdf.ShowDialog() == DialogResult.OK)
{
_inputParameter.FileName = sfd.FileName;
_inputParameter.ProductList = productBindingSource.DataSource as List<product>;
progressBar.Minimum = 0;
progressBar.Value = 0;
backgroundWorker.RunWorkerAsync(_inputParameter);
}
}
}
}
}
UPDATE:
约翰的回答没有解决我的错误,但数据网格目前正在由CS代码而不是数据库填充。如果有人能让我知道他们认为是什么问题,我已经制作了一个视频来更详细地解释问题。
https://www.dropbox.com/s/1l5iw1j32a6oroj/C%23Excel.wmv?dl=0
你好亚历克斯,其他人可能连看都不看这个问题,因为它已经回答了你的问题(连接到访问数据库)与原始文章不同。稍后我会尝试更新我的答案,因为有多种方法可以连接到您的访问数据库。你可以考虑发布另一个关于'Form1_Load'代码的问题,这个代码就是这个连接发生的地方。如果你希望我们可以建立一个聊天,如果它会帮助。 – JohnG
你好约翰,聊天会很棒,我们该如何进行? – UserSN
请给我一分钟,如果可能的话我会尝试着手。 – JohnG