您的位置:首页 > 数据库

将数据库中内容导入/导出为excel

2009-05-11 10:33 375 查看
 
using Microsoft.Office.Interop.Excel;

#region 导出excel,最后一行合并
/// <summary>
/// 导出excel,最后一行合并
/// </summary>
/// <param name="dt"></param>
/// <param name="filePath"></param>
public static void Export(System.Data.DataTable dt, string filePath)
{
if (dt == null)
{
throw new Exception("数据表中无数据");
}
int eRowIndex = 1;
int eColIndex = 1;
int cols = dt.Columns.Count;
int rows = dt.Rows.Count;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
try
{
//列名的处理
for (int i = 0; i < cols; i++)
{
xlApp.Cells[eRowIndex, eColIndex] = dt.Columns[i].ColumnName;
eColIndex++;
}
//列名加粗显示
xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[eRowIndex, cols]).Font.Bold = true;
xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[rows + 1, cols]).Font.Name = "Arial";
xlApp.get_Range(xlApp.Cells[eRowIndex, 1], xlApp.Cells[rows + 1, cols]).Font.Size = "10";
eRowIndex++;

for (int i = 0; i < rows; i++)
{
eColIndex = 1;
for (int j = 0; j < cols; j++)
{
xlApp.Cells[eRowIndex, eColIndex] = dt.Rows[i][j].ToString();
eColIndex++;
}
eRowIndex++;
}

//合并单元格
//ran1.Merge(ran1.MergeCells);
Microsoft.Office.Interop.Excel._Worksheet _workSheet = (_Worksheet)xlApp.ActiveSheet;

Microsoft.Office.Interop.Excel.Range ran1 = _workSheet.get_Range(_workSheet.Cells[dt.Rows.Count + 1, 1], _workSheet.Cells[dt.Rows.Count + 1, 14]);
ran1.MergeCells = true;

//控制单元格中的内容。
xlApp.Cells.EntireColumn.AutoFit();

xlApp.DisplayAlerts = false;
xlBook.SaveCopyAs(filePath);
xlApp.Workbooks.Close();

}
catch
{
throw;
}
finally
{
xlApp.Quit();
//杀掉Excel进程。
GC.Collect();
}
}
#endregion

//杀掉excel进程可能出现问题,参考:http://topic.csdn.net/u/20100520/12/7e077e0b-6336-4972-bd54-c3b48338f9ba.html?33067

/*
//调用

DataSet ds = DataContrl.GetAllMemberForExcel("");

DataRow newRow = ds.Tables[0].NewRow();
//用相应值填写数据行
newRow["姓名"] = ds.Tables[1].Rows[0][0].ToString() ;
newRow["电话号码"] = "";
newRow["分机号码"] = "";
newRow["手机号码"] = "";
newRow["电子邮箱"] = "";
newRow["QQ号码"]="";
newRow["MSN账号"]="";
newRow["阿里旺旺号码"] = "";

//填写完毕,将数据行添加到数据集
ds.Tables[0].Rows.Add(newRow);

DataContrl.Export(ds.Tables[0], @"D:/aa.xls");
*/


--------------------------------------------------------------------------------------------------------------------

Sql 导入 EXCEL 通用存储过程
2008/01/10 15:41
/*
--下面两句在创建存储过程时必须先执行,否则不能创建本存储过程,或者调用出错
SET ANSI_WARNINGS ON--返回警告
GO
SET ANSI_NULLS ON-- 指定在对空值使用等于   (=)   和不等于   (<>)   比较运算符时,这些运算符的   SQL-92   遵从行为。
GO
--将Excel数据导入到sql数据库
--版权 : 启程 letwego.cn
--调用实例
EXEC spExcelOutIn
@strOptions = 'In',
@strWhere = 'WHERE 房号>0',--导入/导出查询条件(包括Where 关键字)
@strExcelPath = 'd:/test.xls',--Excel的绝对路径
@strExcelSheetName = 'Sheet2',--Excel里要导入的工作表名称 如Sheet2,注意,后面不要加上$
@strExcelFiled = '单元编号,单元名称,房号,建筑面积,楼层,楼层名称,楼阁编号,业主',--Excel工作表的字段

@strSqlTableName = 'TExcelOutIn',--Sql数据库导入表名
@strSqlFiled = '单元编号,单元名称,房号编号,建筑面积,楼层,楼层名称,楼阁编号,业主编号'--Sql表的字段
*/

CREATE PROCEDURE spExcelOutIn

@strOptions varchar(200) = NULL,
@strWhere nvarchar(4000) = NULL,--导入/导出查询条件(包括Where 关键字)

@strExcelPath nvarchar(1000) = NULL,--Excel的绝对路径
@strExcelSheetName nvarchar(50) = NULL,--Excel里要导入的工作表名称 如Sheet2,注意,后面不要加上$
@strExcelFiled nvarchar(1000) = NULL,--Excel工作表的字段

@strSqlTableName nvarchar(100) = NULL,--Sql数据库导入/导出表名
@strSqlFiled nvarchar(1000) = NULL--Sql表的字段

AS

DECLARE @strSql nvarchar(4000)

IF OBJECT_ID('tempdb..##Temp') IS NOT NULL DROP TABLE ##Temp

/* 导入 */
IF @strOptions='In' AND NOT @strExcelPath IS NULL BEGIN

SET @strExcelPath = '''Microsoft.Jet.OLEDB.4.0'',''Data Source="' + @strExcelPath + '";User ID=Admin;Password=;Extended properties=Excel 5.0'''

--将数据存放到临时表(一)
SET @strSql =
'SELECT ' + @strExcelFiled +
' INTO ##Temp
FROM
OpenDataSource('+ @strExcelPath +')...'+ @strExcelSheetName +'$ '
+@strWhere

--PRINT(@strSql)
EXEC(@strSql)

--将数据从临时表导入到sql数据库表(二,分两步可以做更多的处理)
SET @strSql = 'INSERT INTO ' + @strSqlTableName + '('+ @strSqlFiled +') SELECT * FROM ##Temp'
--PRINT(@strSql)
EXEC(@strSql)

--SELECT * FROM ##Temp
DROP TABLE ##Temp

END

/* 导出 */
IF @strOptions='Out' AND NOT @strExcelPath IS NULL BEGIN

SET @strExcelPath = ''
--导成类似(不是真正的Excel,是文本格式)Excel的文件,这里是固定的,真正用时需要修改相关参数
--EXEC master..xp_cmdshell 'bcp "SELECT * FROM wy_福州分公司.dbo.TExcelOutIn" queryout C:/test.xls -c -S"(local)" -U"sa" -P"123"'
END
GO

http://topic.csdn.net/u/20090504/21/157ABB4B-1E38-4FDB-9A50-80B440580580.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息