C#æä½EXCELå
¨è§£ï¼ä»£ç ï¼
æ示ï¼è¿è¡ä¹åå¿
é¡»å
å¼ç¨Interop.Excel.dll模å
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Reflection;
using Excel;
namespace AnalysisSystem.DB
{
public class ExcelOperation
{
PRivate string _fileName;//ä¿åè·¯å¾å
public ExcelOperation(string fileName)
{
_fileName = fileName;
}
private OleDbConnection GetConnection()
{
string connectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0",_fileName);
OleDbConnection myConn = new OleDbConnection(connectString);//建ç«é¾æ¥
return myConn;
}
public System.Data.DataTable ExecuteTableResult(string strSql)
{
System.Data.DataTable dt = new System.Data.DataTable();
try
{
OleDbConnection conn = this.GetConnection();
OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn);//æ§è¡ç¸å
³SQLè¯å¥
da.Fill(dt);
}
catch (System.Exception ex)
{
//do nothing
}
return dt;
}
public DataSet ExecuteSetResult(string strSql,string table_name)
{
DataSet ds = new DataSet();
string temp_name = "[" + table_name + "$]";
try
{
OleDbConnection conn = this.GetConnection();
OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn);
da.Fill(ds,temp_name);
}
catch (System.Exception ex)
{
//do nothing
}
return ds;
}
public string ExecuteOneResult(string strSql)
{
string result = "";
System.Data.DataTable dt = new System.Data.DataTable();
try
{
OleDbConnection conn = this.GetConnection();
OleDbDataAdapter da = new OleDbDataAdapter(strSql, conn);
da.Fill(dt);
}
catch (System.Exception ex)
{
//do nothing
}
if (dt != null && dt.Rows.Count > 0)
{
result = dt.Rows[0][0].ToString();
}
return result;
}
public void ExecuteNonResult(string strSql)
{
try
{
OleDbConnection conn = this.GetConnection();
OleDbCommand cmd = new OleDbCommand(strSql, conn);
cmd.ExecuteNonQuery();
}
catch (System.Exception ex)
{
//do nothing
}
}
private _Workbook W_B(Excel.application app)
{
Workbooks workbooks = app.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
return workbook;
}
private _Worksheet W_S(_Workbook work_book)
{
Sheets sheets = work_book.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);//è·åéæ©ç¬¬ä¸ä¸ªè¡¨ï¼æ¬èº«é»è®¤å称为Sheet1
return worksheet;
}
//ä»DataGridViewä¸å¯¼åºæ°æ®å°Excel表,å表导åº
public void Excel_out(DataGridView dataGridView1)
{
//建ç«Excel对象
Excel.Application app = new Excel.Application();
try
{
_Workbook workbook = this.W_B(app);
_Worksheet worksheet = this.W_S(workbook);
string sLen = "";
//åå¾æåä¸ååå
char H = (char)(64 + dataGridView1.ColumnCount / 26);
char L = (char)(64 + dataGridView1.ColumnCount % 26);
if (dataGridView1.ColumnCount < 26)
{
sLen = L.ToString();
}
else
{
sLen = H.ToString() + L.ToString();
}
//æ é¢
string sTmp = sLen + "1";
Range ranCaption = worksheet.get_Range(sTmp, "A1");
string[] asCaption = new string[dataGridView1.ColumnCount];
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
asCaption[i] = dataGridView1.Columns[i].HeaderText;
}
ranCaption.Value2 = asCaption;
//æ°æ®
object[] obj = new object[dataGridView1.Columns.Count];
for (int r = 0; r < dataGridView1.RowCount - 1; r++)
{
for (int l = 0; l < dataGridView1.Columns.Count; l++)
{
if (dataGridView1[l, r].ValueType == typeof(DateTime))
{
obj[l] = dataGridView1[l, r].Value.ToString();
}
else
{
obj[l] = dataGridView1[l, r].Value;
}
}
string cell1 = sLen + ((int)(r + 2)).ToString();
string cell2 = "A" + ((int)(r + 2)).ToString();
Range ran = worksheet.get_Range(cell1, cell2);
ran.Value2 = obj;
}
//ä¿å
workbook.SaveCopyAs(this._fileName);
workbook.Saved = true;
}
finally
{
//å
³é
app.UserControl = false;
app.Quit();
}
}
/// <summary>
/// å¤è¡¨å¯¼åº
/// </summary>
/// <param name="dataGridView">DataGridViewå表éå</param>
/// <param name="TableList">表å称éå</param>
public void Excel_out_MulTable(List<DataGridView> dataGridView, string[] TableList)
{
//建ç«Excel对象
Excel.Application app = new Excel.Application();
try
{
Workbooks workbooks = app.Workbooks;//å®ä¹ä¸ä¸ªå·¥ä½ç°¿éå
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);//åå·¥ä½ç°¿æ·»å ä¸ä¸ªæ°å·¥ä½ç°¿
Sheets sheets = workbook.Worksheets;//å®ä¹ä¸ä¸ªå·¥ä½è¡¨éå
_Worksheet worksheet ;
int wnumber = 0;
while (wnumber++ < (TableList.GetLength(0) - 1))
{
sheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);//åä¸ä¸ªå·¥ä½è¡¨éåæ·»å ä¸ä¸ªæ°å·¥ä½è¡¨
}
/*æéï¼Missing类为å½å空é´System.Reflectionä¸çç±»ï¼æ以记å¾å¼å
¥*/
wnumber = 0;
foreach (DataGridView dataGridView1 in dataGridView)
{
worksheet = null;
worksheet = (_Worksheet)sheets.get_Item(wnumber + 1);//ååºéè¦è¿è¡æä½çå·¥ä½è¡¨
worksheet.Name = TableList[wnumber];//设置æ¹å·¥ä½è¡¨å称
if (wnumber != 0)
sheets.Select(wnumber);//éä¸æä½è¡¨
string sLen = "";
//åå¾æåä¸ååå
char H = (char)(64 + dataGridView1.ColumnCount / 26);
char L = (char)(64 + dataGridView1.ColumnCount % 26);
if (dataGridView1.ColumnCount < 26)
{
sLen = L.ToString();
}
else
{
sLen = H.ToString() + L.ToString();
}
//æ é¢
string sTmp = sLen + "1";
Range ranCaption = worksheet.get_Range(sTmp, "A1");
string[] asCaption = new string[dataGridView1.ColumnCount];
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
asCaption[i] = dataGridView1.Columns[i].HeaderText;
}
ranCaption.Value2 = asCaption;
//æ°æ®
object[] obj = new object[dataGridView1.Columns.Count];
for (int r = 0; r < dataGridView1.RowCount - 1; r++)
{
for (int l = 0; l < dataGridView1.Columns.Count; l++)
{
if (dataGridView1[l, r].ValueType == typeof(DateTime))
{
obj[l] = dataGridView1[l, r].Value.ToString();
}
else
{
obj[l] = dataGridView1[l, r].Value;
}
}
string cell1 = sLen + ((int)(r + 2)).ToString();
string cell2 = "A" + ((int)(r + 2)).ToString();
Range ran = worksheet.get_Range(cell1, cell2);//设置åå
æ ¼
ran.Value2 = obj;
}
wnumber++;
}
//ä¿å
workbook.SaveCopyAs(this._fileName);
workbook.Saved = true;
}
finally
{
//å
³é
app.UserControl = false;
app.Quit();
}
}
}
}
温馨提示:答案为网友推荐,仅供参考