调用存储过程取到数据通过NPOI存到Excel中
2014-06-14 17:08
531 查看
//调用
public ActionResult GenerateExcel() {
DataTable headerTable = new DataTable();
headerTable.Columns.Add("Status_Id", Type.GetType("System.String"));
headerTable.Columns.Add("Status_Name", Type.GetType("System.String"));
headerTable.Columns.Add("Is_Now", Type.GetType("System.String"));
headerTable.Columns.Add("Curr_User_Id", Type.GetType("System.String"));
headerTable.Columns.Add("Flow_Id", Type.GetType("System.String"));
DataRow headerRow = headerTable.NewRow();
headerRow["Status_Id"] = "Status_Id";
headerRow["Status_Name"] = "Status_Name";
headerRow["Is_Now"] = "Is_Now";
headerRow["Curr_User_Id"] = "Curr_User_Id";
headerRow["Flow_Id"] = "Flow_Id";
headerTable.Rows.Add(headerRow);
DataTable contentTable = getDataByProc();
generateExcelOP(headerTable, contentTable);
ViewData["result"] = "Create successfully";
return View("ExcelUpload");
}
//生成Excel
public void generateExcelOP(DataTable headerTable,DataTable dt) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet() as HSSFSheet;
//excel header info
HSSFRow headerRow = sheet.CreateRow(0) as HSSFRow;
for(int i = 0 ;i < headerTable.Columns.Count;i++){
HSSFCell headerCell = headerRow.CreateCell(i) as HSSFCell;
headerCell.SetCellValue(headerTable.Rows[0][i].ToString());
headerRow.Cells.Add(headerCell);
}
//excel content info
for(int m =0 ;m < dt.Rows.Count;m++){
HSSFRow row = sheet.CreateRow((m+1)) as HSSFRow;
for (int n = 0; n < dt.Columns.Count;n++ )
{
HSSFCell cell = row.CreateCell(n) as HSSFCell;
cell.SetCellValue(dt.Rows[m]
.ToString());
row.Cells.Add(cell);
}
}
FileStream fs = new FileStream(@"D:\data\output.xls", FileMode.Create);
workbook.Write(fs);
fs.Flush();
fs.Close();
workbook.Clear();
}
//获取数据
public DataTable getDataByProc() {
DataTable dt = new DataTable();
string callName = "GetAuthHistory";
string devConn = System.Configuration.ConfigurationManager.ConnectionStrings["devConn"].ToString();
using(SqlConnection conn = new SqlConnection(devConn)){
SqlCommand command = new SqlCommand(callName, conn);
command.CommandType = CommandType.StoredProcedure;
SqlParameter[] sps = {
new SqlParameter("@currPage",SqlDbType.Int),
new SqlParameter("@pageSize",SqlDbType.Int)
};
sps[0].Value = 1;
sps[1].Value = 20;
foreach(SqlParameter sp in sps){
command.Parameters.Add(sp);
}
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = command;
sda.Fill(dt);
}
return dt;
}
public ActionResult GenerateExcel() {
DataTable headerTable = new DataTable();
headerTable.Columns.Add("Status_Id", Type.GetType("System.String"));
headerTable.Columns.Add("Status_Name", Type.GetType("System.String"));
headerTable.Columns.Add("Is_Now", Type.GetType("System.String"));
headerTable.Columns.Add("Curr_User_Id", Type.GetType("System.String"));
headerTable.Columns.Add("Flow_Id", Type.GetType("System.String"));
DataRow headerRow = headerTable.NewRow();
headerRow["Status_Id"] = "Status_Id";
headerRow["Status_Name"] = "Status_Name";
headerRow["Is_Now"] = "Is_Now";
headerRow["Curr_User_Id"] = "Curr_User_Id";
headerRow["Flow_Id"] = "Flow_Id";
headerTable.Rows.Add(headerRow);
DataTable contentTable = getDataByProc();
generateExcelOP(headerTable, contentTable);
ViewData["result"] = "Create successfully";
return View("ExcelUpload");
}
//生成Excel
public void generateExcelOP(DataTable headerTable,DataTable dt) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet() as HSSFSheet;
//excel header info
HSSFRow headerRow = sheet.CreateRow(0) as HSSFRow;
for(int i = 0 ;i < headerTable.Columns.Count;i++){
HSSFCell headerCell = headerRow.CreateCell(i) as HSSFCell;
headerCell.SetCellValue(headerTable.Rows[0][i].ToString());
headerRow.Cells.Add(headerCell);
}
//excel content info
for(int m =0 ;m < dt.Rows.Count;m++){
HSSFRow row = sheet.CreateRow((m+1)) as HSSFRow;
for (int n = 0; n < dt.Columns.Count;n++ )
{
HSSFCell cell = row.CreateCell(n) as HSSFCell;
cell.SetCellValue(dt.Rows[m]
.ToString());
row.Cells.Add(cell);
}
}
FileStream fs = new FileStream(@"D:\data\output.xls", FileMode.Create);
workbook.Write(fs);
fs.Flush();
fs.Close();
workbook.Clear();
}
//获取数据
public DataTable getDataByProc() {
DataTable dt = new DataTable();
string callName = "GetAuthHistory";
string devConn = System.Configuration.ConfigurationManager.ConnectionStrings["devConn"].ToString();
using(SqlConnection conn = new SqlConnection(devConn)){
SqlCommand command = new SqlCommand(callName, conn);
command.CommandType = CommandType.StoredProcedure;
SqlParameter[] sps = {
new SqlParameter("@currPage",SqlDbType.Int),
new SqlParameter("@pageSize",SqlDbType.Int)
};
sps[0].Value = 1;
sps[1].Value = 20;
foreach(SqlParameter sp in sps){
command.Parameters.Add(sp);
}
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = command;
sda.Fill(dt);
}
return dt;
}
相关文章推荐
- Java调用oracle存储过程通过游标返回临时表数据
- [转]SQLSERVER存储过程调用不同数据库的数据_存储过程中通过链接服务器访问远程服务器
- 通过调用过程把图片文存储到数据
- 客户端如何通过咏南中间件调用存储过程和数据分页查询和文件传输的演示
- VB.NET、EXCEL调用SQL Server带参数的存储过程并返回数据集
- 通过java调用存储过程往oracle数据库里插数据(无返回值)
- Java调用oracle存储过程通过游标返回临时表数据
- Java调用oracle存储过程通过游标返回临时表数据
- 通过程序调用存储过程
- 存储过程调用DTS包实现大批量数据导入
- ASP.NET通过存储过程上传Excel中的内容到Oracle数据库
- 如何在aSP中通过OccC调用Excel中的数据
- 数据做为存储过程参数在JAVA中的调用
- 存储过程调用,java中通过bboss persistent实现数据库存储过程的调用(命名变量绑定)
- 调用存储过程,java中通过bboss persistent实现数据库存储过程的调用(位置变量绑定)
- 在.net中如何把调用存储过程代码写入数据连接层中
- 程序调用查询数据存储过程的问题
- 通过ADOQuery调用SQLServer的存储过程
- proc 存储过程,通过存储过程往数据库的某个表中插入指定数据量的纪录,表中有primary key
- .Nettiers的事件:下拉控件数据绑定,Insert,调用自定存储过程,获取新增ID,事务,DeepLoad,DeepSave,Save,Update,DeepDelete【原创】