话不多说我直接进入正题,疫情期间先来无事,找点事做。
由于自己写入2007版xlsx,总是出问题后来查到是.xlsx推荐用EPPLUS,xls推荐用NPOI,还以为自己代码写的有问题,气死我了!!!
1.首先下载npoI组件,后引用对应的命名空间,在Userball中。
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
2.创建一个类sqlhelper ,读取dataTable的函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace sannCeng.DAL33
{
class sqlHelper33
{
//获取配置文件
private static string connStr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
public static DataTable ExecuteQuery(string sql, params SqlParameter[] sqlParameters)
{
using (SqlConnection conn = new SqlConnection(connStr))
using (SqlCommand cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = sql;
cmd.Parameters.AddRange(sqlParameters);
DataTable dt = new DataTable();
using (SqlDataReader reader = cmd.ExecuteReader())
{
dt.Load(reader);
}
return dt;
}
}
}
}
3.创建一个实现类UserDal调用ExecuteQuery
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using sanCeng.Model33;
using System.Data;
namespace sannCeng.DAL33
{
public class UserDal
{
public DataTable GetAll()
{
DataTable dataTable = sqlHelper33.ExecuteQuery("select * from T_users p where p.Isdeleted=0");
return dataTable;
}
}
}
4.创建一个UserBll调用UserDal
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using sanCeng.Model33;
using sannCeng.DAL33;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Data;
using System.IO;
namespace sanCeng.BLL33
{
public class UserBll
{
private UserDal uDAL = new UserDal();
public void GetAll()
{
DataTable tmpDataTable = uDAL.GetAll();
string TargetFileNamePath = @"C:\Users\xg\Desktop\test.xls";//目标文件excel的路径
// string sheetName;//excel表中的sheet的名称,可以根据情况自己起
int columnIndex = 0;
int rowNum = tmpDataTable.Rows.Count;//总行数
int columnNum = tmpDataTable.Columns.Count;//总列数
if (tmpDataTable == null)
{
return;
}
// 数据验证
if (!File.Exists(TargetFileNamePath))
{
//excel文件的路径不存在
throw new ArgumentException("excel文件的路径不存在或者excel文件没有创建好");
}
//根据Excel文件的后缀名创建对应的workbook
IWorkbook xlApp = null;
if (TargetFileNamePath.IndexOf(".xlsx") > 0)
{ //2007版本的excel
xlApp = new XSSFWorkbook();
}
else if (TargetFileNamePath.IndexOf(".xls") > 0) //2003版本的excel
{
xlApp = new HSSFWorkbook();
}
//IWorkbook xlApp = new XSSFWorkbook();
//excel表的sheet名
ISheet sheet = xlApp.CreateSheet("user");
// ISheet sheet2 = xlApp.GetSheetAt(0);
IRow row = sheet.CreateRow(0);
// int lastRowNumber = sheet.LastRowNum;
foreach (DataColumn dc in tmpDataTable.Columns)
{
// row.GetRow(rowIndex).GetCell(columnIndex).SetCellValue();
row.CreateCell(columnIndex).SetCellValue(dc.ColumnName.ToString());
columnIndex++;
// row[rowIndex, columnIndex] = dc.ColumnName;
// row[rowIndex, columnIndex] = dc.ColumnName;
}
//写入数据
for (int row1 = 1; row1 < tmpDataTable.Rows.Count; row1++)
{
//sheet表创建新的一行
IRow newRow = sheet.CreateRow(row1);
for (int column = 0; column < tmpDataTable.Columns.Count; column++)
{
newRow.CreateCell(column).SetCellValue(tmpDataTable.Rows[row1][column].ToString());
}
}
//写入到excel中
using (Stream fileStream = File.OpenWrite(TargetFileNamePath))
{
xlApp.Write(fileStream);
fileStream.Close();
}
}
}
}
网友评论