C#操作excel
- 引用的com组件:Microsoft.Office.Interop.Excel.dll,一般安装完office就有这个文件。(office 2003下载这个dll:点击下载)。
- 新建一个C#项目,添加前面的dll到引用里面来
- 读取excel数据到dataset
方法一
private void OpenExcel(string strFileName)
{
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass appc = new Microsoft.Office.Interop.Excel.ApplicationClass();//lauch excel application
if (appc == null)
{
MessageBox.Show("can't access excel");
}
else
{
// Microsoft.Office.Interop.Excel.Workbook wb = appc.Application.Workbooks.Add(true);
//open excel file
Microsoft.Office.Interop.Excel.Workbook wb = appc.Application.Workbooks.Open(strFileName, missing, false, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing);
//get first worksheet
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
//get range from A1 to A3 Microsoft.Office.Interop.Excel.Range rng = ws.Cells.get_Range("A1", "A3");
object[,] arrValue = (object[,])rng.Value2;//get range's value
string strValue = "";
for (int i = 1; i <= arrValue.GetLength(0); i++)
{
strValue +="Cell"+i+":"+ arrValue[i, 1].ToString()+System.Environment.NewLine;
}
MessageBox.Show(strValue.ToString());//show value
}
appc.Quit();
appc = null;
}
方法二
public DataSet ExcelToDS(string Path)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel="select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds,"table1");
return ds;
}
- dataset导出到excel
4.1 写数据
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass appc = new Microsoft.Office.Interop.Excel.ApplicationClass();//lauch excel
Microsoft.Office.Interop.Excel.Workbook wb = appc.Application.Workbooks.Add(true);//add new workbook Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);//get first worksheet
Microsoft.Office.Interop.Excel.Range range = ws.get_Range("A1", "C1");//get range from A1 to C1
string[] arrValue={"1","2","3"};
range.Value2 = arrValue;//set value
.
或者
public bool SaveFP2toExcel(string Path)
{
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.OleDb.OleDbCommand cmd=new OleDbCommand ();
cmd.Connection =conn;
//cmd.CommandText ="UPDATE [sheet1$] SET 姓名='2005-01-01' WHERE 工号='日期'";
//cmd.ExecuteNonQuery ();
for(int i=0;i<fp2.Sheets [0].RowCount -1;i++)
{
if(fp2.Sheets [0].Cells[i,0].Text!="")
{
cmd.CommandText ="INSERT INTO [sheet1$] (工号,姓名,部门,职务,日期,时间) VALUES('"+fp2.Sheets [0].Cells[i,0].Text+ "','"+
fp2.Sheets [0].Cells[i,1].Text+"','"+fp2.Sheets [0].Cells[i,2].Text+"','"+fp2.Sheets [0].Cells[i,3].Text+
"','"+fp2.Sheets [0].Cells[i,4].Text+"','"+fp2.Sheets [0].Cells[i,5].Text+"')";
cmd.ExecuteNonQuery ();
}
}
conn.Close ();
return true;
}
catch(System.Data.OleDb.OleDbException ex)
{
System.Diagnostics.Debug.WriteLine ("写入Excel发生错误:"+ex.Message );
}
return false;
}
4.2 格式化
设置字体颜色
range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);//white color
设置背景色
range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.MidnightBlue);//set backgroud color
posted on
2008-04-06 20:57
Agan@CN
阅读(
2668)
评论()
编辑
收藏
举报