将数据库中内容导入/导出为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
相关文章推荐
- 通过SQL语句直接实现Excel与数据库的导入导出
- 将数据库内容导出到Excel-------Spreadsheet
- 用sql server导入导出功能 把excel导入到sql的表中,提示导入成功,但是数据库表中并没有数据?
- C#数据库数据导入导出系列之四 WinForm数据库导入导出到Excel
- JXL导入导出Excel到数据库
- POI 导入导出Excel文件到数据库(转载)
- 通过SQL语句直接实现Excel与数据库的导入导出
- POI Excel 导入导出数据库
- 数据库内容导出到EXCEL生成报表和图形的方法
- 数据导入,可以将txt文本文件或excel的内容导入到数据库
- 将Excel内容导入数据库(ASP.NET/C#)
- 将数据库中的内容导入到excel
- 数据库内容导出为excel并下载
- EXcel 导入数据库和导出数据库的代码
- 发一个通用从数据库导出excel、excel导入数据库组件
- 转发一个通用从数据库导出excel、excel导入数据库组件
- SQL语句导入/导出Excel 数据库
- JAVA实现数据库数据导入/导出到Excel(POI技术)
- java从EXCEL导入到数据库,从数据库导出到Excel
- 通过SQL语句直接实现Excel与数据库的导入导出