C#读取EXCEL模版方法
2012-08-14 20:04
369 查看
//读取EXCEL模版
FileStream fs = new FileStream(tempName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
ISheet sheet = hssfworkbook.GetSheetAt(0);
//向Excel模版写标题
sheet.GetRow(Convert.ToInt16(indexCaption[0])).GetCell(Convert.ToInt16(indexCaption[1])).SetCellValue(tmpReportDate);
DataTable dt = new DataTable();
//连接数据库
SqlConnection sqlconn = new SqlConnection(DbProvider.ConnectionString);
SqlCommand sqlcomm = sqlconn.CreateCommand();
sqlconn.Open();
sqlcomm.CommandType = CommandType.StoredProcedure;
sqlcomm.Parameters.Add("@pmDate", SqlDbType.NVarChar).Value = curDate;
sqlcomm.Parameters.Add("@pmProductCode", SqlDbType.NVarChar).Value = "";
sqlcomm.Parameters.Add("@pmBranchCode", SqlDbType.NVarChar).Value = "";
if (IndexSystemID.Length > 0) sqlcomm.Parameters.Add("@pmSystemCode", SqlDbType.NVarChar).Value = "";
sqlcomm.Parameters.Add("@pmLX", SqlDbType.NVarChar).Value = "";
for (int s = 0; s < storeName.Length; s++)
{
sqlcomm.Parameters["@pmProductCode"].Value = indexProductID[s];
sqlcomm.Parameters["@pmBranchCode"].Value = indexBranchID[s];
if (IndexSystemID.Length > 0) sqlcomm.Parameters["@pmSystemCode"].Value = IndexSystemID[s];
sqlcomm.Parameters["@pmLX"].Value = pmType;
sqlcomm.CommandText = storeName[s];
dt.Load(sqlcomm.ExecuteReader());
if (dt.Rows.Count > 0)
{
int i = 0,j;
for (int r = Convert.ToInt16(indexLocation[s * 4]); r < dt.Rows.Count + Convert.ToInt16(indexLocation[s * 4]); r++, i++)
{
j = 0;
IRow row = sheet.GetRow(r);
for (int col = Convert.ToInt16(indexLocation[s * 4 + 2]); col < Convert.ToInt16(indexLocation[s * 4 + 2]) + Convert.ToInt16(indexLocation[s * 4 + 3]); col++, j++)
row.GetCell(col).SetCellValue(dt.Rows[i][j].ToString());
}
}
//将DataTable清空,如果不清空将会导致dt的数据累计
dt.Clear();
}
//把工作簿写入文件
using (FileStream wfs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
hssfworkbook.Write(wfs);
wfs.Close();
//downFile(fileName);
}
sqlconn.Close();
hssfworkbook.Dispose();
FileStream fs = new FileStream(tempName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
ISheet sheet = hssfworkbook.GetSheetAt(0);
//向Excel模版写标题
sheet.GetRow(Convert.ToInt16(indexCaption[0])).GetCell(Convert.ToInt16(indexCaption[1])).SetCellValue(tmpReportDate);
DataTable dt = new DataTable();
//连接数据库
SqlConnection sqlconn = new SqlConnection(DbProvider.ConnectionString);
SqlCommand sqlcomm = sqlconn.CreateCommand();
sqlconn.Open();
sqlcomm.CommandType = CommandType.StoredProcedure;
sqlcomm.Parameters.Add("@pmDate", SqlDbType.NVarChar).Value = curDate;
sqlcomm.Parameters.Add("@pmProductCode", SqlDbType.NVarChar).Value = "";
sqlcomm.Parameters.Add("@pmBranchCode", SqlDbType.NVarChar).Value = "";
if (IndexSystemID.Length > 0) sqlcomm.Parameters.Add("@pmSystemCode", SqlDbType.NVarChar).Value = "";
sqlcomm.Parameters.Add("@pmLX", SqlDbType.NVarChar).Value = "";
for (int s = 0; s < storeName.Length; s++)
{
sqlcomm.Parameters["@pmProductCode"].Value = indexProductID[s];
sqlcomm.Parameters["@pmBranchCode"].Value = indexBranchID[s];
if (IndexSystemID.Length > 0) sqlcomm.Parameters["@pmSystemCode"].Value = IndexSystemID[s];
sqlcomm.Parameters["@pmLX"].Value = pmType;
sqlcomm.CommandText = storeName[s];
dt.Load(sqlcomm.ExecuteReader());
if (dt.Rows.Count > 0)
{
int i = 0,j;
for (int r = Convert.ToInt16(indexLocation[s * 4]); r < dt.Rows.Count + Convert.ToInt16(indexLocation[s * 4]); r++, i++)
{
j = 0;
IRow row = sheet.GetRow(r);
for (int col = Convert.ToInt16(indexLocation[s * 4 + 2]); col < Convert.ToInt16(indexLocation[s * 4 + 2]) + Convert.ToInt16(indexLocation[s * 4 + 3]); col++, j++)
row.GetCell(col).SetCellValue(dt.Rows[i][j].ToString());
}
}
//将DataTable清空,如果不清空将会导致dt的数据累计
dt.Clear();
}
//把工作簿写入文件
using (FileStream wfs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
hssfworkbook.Write(wfs);
wfs.Close();
//downFile(fileName);
}
sqlconn.Close();
hssfworkbook.Dispose();
相关文章推荐
- C#读取Excel 几种方法的体会
- 关于在C#中读取导入的Excel一些值为空的解决方法
- c#读取excel的xls和xlsx文件的方法
- C#读取Excel方法之一
- c# 解决读取Excel混合文本类型,数据读取失败的解决方法
- C# 读取EXCEL文件的三种经典方法
- C# 用数据库读取Excel出现“定义了过多字段”错误的解决方法 .
- 用c#读取Excel的方法
- 运用C#读取导入的Excel部分数据为空的解决方法
- C# 用数据库读取Excel出现“定义了过多字段”错误的解决方法
- C# ----Excel读取数据之数据引擎方法
- C#_读取EXCEL文件的三种经典方法
- C#读取Excel几种方法的体会
- C#读取Excel 几种方法的体会
- C#读取Excel的方法
- C#读取Excel到DataSet的方法
- C#读取Excel遇到无法读取的解决方法
- C#中Excel文件的读取的两种方法:Excel类和OleDb
- C# 用数据库读取Excel出现“定义了过多字段”错误的解决方法
- C# 读取EXCEL文件的三种经典方法