using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.IO;
namespace exporttoexcel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnExport_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application();
Workbook wb = xls.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xls.ActiveSheet;
xls.Visible = true;
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
{
for (int j = 0; j <= dataGridView1.ColumnCount ; j++)
{
DataGridViewCell cell = dataGridView1[j, i];
ws.Cells[i + 1, j + 1] = cell.Value;
}
}
ws.Cells[1, 6] = "Power Input(W)";
ws.Cells[2, 6] = "txtPin.Text";
//changing the name of active sheet
ws.Name = "Characteristic of Solar cells";
//storing each row and column value to excel sheet
for (int i = 0; i <= dataGridView1.RowCount-2; i++)
{
for (int j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView1[j, i];
ws.Cells[i + 1, j + 1] = cell.Value;
}
}
}
private void but_read_Click(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'database2DataSet.Table1' table. You can move, or remove it, as needed.
this.table1TableAdapter.Fill(this.database2DataSet.Table1);
but_read.Enabled = false;
btnExport.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
btnExport.Enabled = false;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb; //เพื่อติดต่อฐานข้อมูลโดยใช้คำสั่ง OleDb
using Excel = Microsoft.Office.Interop.Excel; //เพื่อนำข้อมูลเข้า Excel
using System.IO;
namespace Excel
{
public partial class Welcome : Form
{
public Welcome()
{
InitializeComponent();
}
//ชนิดของฐานข้อมูลและที่อยู่ของข้อมูล
int flag = 0;
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + @"Data Source=D:\study\Database2.accdb";//เชื่อมกับฐานข้อมูล acess
//เมื่อกดปุ่ม Read
#region FunctionRead
private void but_read_Click(object sender, EventArgs e)
{
OleDbConnection Conn = new OleDbConnection(strConn);
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
if (Conn.State == ConnectionState.Open)
{
//Function for Read Data
string sql = "SELECT * FROM Table1";
OleDbCommand cmd = new OleDbCommand(sql, Conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
DataSet data = new DataSet();
adapter.Fill(data, "Tab1");
}
Conn.Close();
but_read.Enabled = false;
but_ok.Enabled = true;
}
#endregion
#region FunctionExport_to_Excel
private void but_ok_Click(object sender, EventArgs e)
{
OleDbConnection Conn = new OleDbConnection(strConn);
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
if (Conn.State == ConnectionState.Open)
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel._Application(); // creating Excel Application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); // creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Worksheet worksheet = null; // creating new Excelsheet in workbook
app.Visible = true; // see the excel sheet behind the program
try
{
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["sheet1"];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
//changing the name of active sheet
worksheet.Name = "Characteristic of Solar cells";
//storing header part in Excel
for (int i = 1; i < DataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = DataGridView1.Columns[i - 1].HeaderText;
}
//storing each row and column value to excel sheet
for (int i = 0; i < DataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < DataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = DataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
}
} //ส่วนนี้ errorCS1524 อะคับ
}
#endregion
private void Welcome_Load(object sender, EventArgs e)
{
but_ok.Enabled = false;
//ตั้งค่าส่วน Connect
OleDbConnection Conn = new OleDbConnection(strConn);
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
if (Conn.State == ConnectionState.Open)
{
//Function for Read Data
string sql = "SELECT * FROM Table1";
OleDbCommand cmd = new OleDbCommand(sql, Conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
DataSet data = new DataSet();
adapter.Fill(data, "Tab1");
//Read จำนวน Number
int Newcount = data.Tables[""].Rows.Count;
DataGridView1.DataSource = data.Tables[0];
}
else
{
//ถ้าเชื่อมต่อไม่ได้
la_process.Text = "Can't Connect";
}
}
}
}
พี่คับมัน
Code
D:\study\My Project\Access\excel\Excel\Excel\Program.cs(18,33): error CS0246: The type or namespace name 'Welcome' could not be found (are you missing a using directive or an assembly reference?)
D:\study\My Project\Access\excel\Excel\Excel\Form1.cs(96,13): error CS1524: Expected catch or finally
โดยที่
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Excel_export
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Welcome()); // มันerror CS0246 ตรงนี้อะคับ
}
}
}