分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility (续篇)

上周六我发表的文章《分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility》受到了大家的热烈支持与推荐,再此表示感谢,该ExcelUtility类库自上次发文起,又经过了多次的改进,增加了许多的功能与方法,可以全面满足大家的需求,下面先来看一下新方法的测试结果:

第一个新增功能,列宽自适应,当超过30个字符则将单元格内容设为换行

任意一个无模板的导出方法均支持该功能,示例代码如下:

?

1

2

3

4

5

6

7

扫描二维码关注公众号,回复: 6186243 查看本文章

8

9

10

/// <summary>

/// 测试方法:测试将DataTable导出到EXCEL,无模板

/// </summary>

[TestMethod]

public void TestExportToExcelByDataTable()

{

    DataTable dt = GetDataTable();

    string excelPath = ExcelUtility.Export.ToExcel(dt, "导出结果");

    Assert.IsTrue(File.Exists(excelPath));

}

结果如下图示:

第二个新增功能,依据数据源(DataTable、DataGridView)的列类型自动将与之对应的EXCEL列的单元格式设为相同的格式内容显示,如:整数类型显示在单元格内无小数的数字格式,有小数位的类显示在单元格内2位小数数字格式,日期类型显示在单元格内日期+时间的日期格式,布尔类型显示在单元格内布尔格式,任意一个无模板的导出方法均支持该功能,示例代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

/// <summary>

/// 测试方法:测试将DataTable导出到EXCEL,无模板,且指定导出的列名,以及导出列名的重命名

/// </summary>

[TestMethod]

public void TestExportToExcelByDataTable3()

{

    DataTable dt = GetDataTable();

    string[] expColNames = { "Col1", "Col2", "Col3", "Col4", "Col5", "Col7" };

    Dictionary<string, string> expColAsNames = new Dictionary<string, string>() {

        {"Col1","列一"},

        {"Col2","列二"},

        {"Col3","列三"},

        {"Col4","数字列"},

        {"Col5","列五"},

        {"Col7","日期列"}

    };

    string excelPath = ExcelUtility.Export.ToExcel(dt, "导出结果", null, expColNames, expColAsNames);

    Assert.IsTrue(File.Exists(excelPath));

}

结果如下图示:

第三个新增功能,在第二个新增功能的基础上,增加可以自定义设置列的单元格显示格式(支持日期类型、数字类型),任意一个无模板的导出方法均支持该功能,示例代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

/// <summary>

/// 测试方法:测试将DataTable导出到EXCEL,无模板,且指定某些列的显示格式

/// </summary>

[TestMethod]

public void TestExportToExcelByDataTable6()

{

    DataTable dt = GetDataTable();

    var colDataFormatDic = new Dictionary<string, string>

    {

        {"Col4","0.000"}, //将Col4列DOUBLE类型的EXCEL对应列格式设置为显示成3位小数(默认为2位小数)

        {"Col7","yyyy-mm-dd"}//将Col7列DateTime类型的EXCEL对应列格式设置为年月日(默认为yyyy/mm/dd hh:mm:ss)

    };

    //更多设置格式可在EXCEL的设置单元格格式中的数字选项卡中的自定义格式列表(若无,可自定义,建议先在EXCEL中测试好格式字符串后再用于程序中)

    string excelPath = ExcelUtility.Export.ToExcel(dt, "导出结果", colDataFormats: colDataFormatDic);

    Assert.IsTrue(File.Exists(excelPath));

}

结果如下图示:

换种格式定义测试:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

/// <summary>

/// 测试方法:测试将DataTable导出到EXCEL,无模板,且指定某些列的显示格式

/// </summary>

[TestMethod]

public void TestExportToExcelByDataTable7()

{

    DataTable dt = GetDataTable();

    var colDataFormatDic = new Dictionary<string, string>

    {

        {"Col4","¥#,##0.00_);(¥#,##0.00)"}, //将Col4列DOUBLE类型的EXCEL对应列格式设置为显示成包含货币格式,如:¥5.00(默认为2位小数)

        {"Col7","yyyy\"年\"m\"月\"d\"日\";@"}//将Col7列DateTime类型的EXCEL对应列格式设置为中文年月日,如:2015年12月5日(默认为yyyy/mm/dd hh:mm:ss)

    };

    //更多设置格式可在EXCEL的设置单元格格式中的数字选项卡中的自定义格式列表(若无,可自定义,建议先在EXCEL中测试好格式字符串后再用于程序中)

    string excelPath = ExcelUtility.Export.ToExcel(dt, "导出结果", colDataFormats: colDataFormatDic);

    Assert.IsTrue(File.Exists(excelPath));

}

结果如下图示:

注意事项说明:想要实现导出的EXCEL单元格依据数据类型自动设置或手动指定格式,需首先确保数据源的列与自动或手动设置的格式相符,即列类型必需是数字类型、日期类型、布尔类型,不能是以字符串的形式存在的这些所谓的“数字类型、日期类型、布尔类型”


第四个新增的功能,可指定DataGridView是否可以导出隐藏列(不显示的列)、及指定依据DataGridView标题列名导出相应列数据,示例代码如下:

?

1

2

3

4

5

6

7

8

9

10

/// <summary>

/// 测试方法:测试将DataGridView数据导出到EXCEL文件,无模板,且不导出隐藏列

/// </summary>

[TestMethod]

public void TestToExcelByDataGridView()

{

    var grid = GetDataGridViewWithData();

    string excelPath = ExcelUtility.Export.ToExcel(grid, "导出结果", null, false);

    Assert.IsTrue(File.Exists(excelPath));

}

结果如下图示:

第五个新增功能,DataGridView若改变列的显示位置,导出的数据也能与界面显示的数据同步调整,示例代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

/// <summary>

/// 测试方法:测试将DataGridView数据导出到EXCEL文件,无模板,改变列的显示位置,导出隐藏列

/// </summary>

[TestMethod]

public void TestToExcelByDataGridView2()

{

    var grid = GetDataGridViewWithData();

    //模拟改变列的显示位置

    grid.Columns[0].DisplayIndex = 1;

    grid.Columns[1].DisplayIndex = 0;

    string excelPath = ExcelUtility.Export.ToExcel(grid, "导出结果", null, true);

    Assert.IsTrue(File.Exists(excelPath));

}

结果如下图示:

以下是GetDataGridViewWithData模拟数据方法:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

private DataGridView GetDataGridViewWithData()

{

    var grid = new DataGridView();

    var dt = GetDataTable();

    foreach (DataColumn col in dt.Columns)

    {

        bool v = col.Ordinal > 4 ? false : true;

        grid.Columns.Add(new DataGridViewTextBoxColumn() { DataPropertyName = col.ColumnName, HeaderText ="列名" + col.ColumnName , Visible = v,ValueType=col.DataType });

    }

    foreach (DataRow row in dt.Rows)

    {

        ArrayList values = new ArrayList();

        foreach (DataColumn col in dt.Columns)

        {

            values.Add(row[col]);

        }

        grid.Rows.Add(values.ToArray());

    }

    return grid;

}

  

我相信这些功能加上上次的功能,应该能满足大家日常工作中所遇到的各种导出EXCEL场景吧,下面重新公布一下两个核心的与导出相关类源代码,以供大家参考,若有不足之处,敬请指出,谢谢!

ExcelUtility.Export:

+ View Code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

usingExcelReport;

usingExcelUtility.Base;

usingNPOI.SS.UserModel;

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Data;

usingSystem.IO;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceExcelUtility

{

    /// <summary>

    /// EXCEL导出功能集合类

    /// 作者:Zuowenjun

    /// 日期:2016/1/15

    /// </summary>

    publicsealedclassExport

    {

        /// <summary>

        /// 由DataSet导出Excel

        /// </summary>

        /// <param name="sourceTable">要导出数据的DataTable</param>

        /// <param name="filePath">导出路径,可选</param>

        /// <returns></returns>

        publicstaticstringToExcel(DataSet sourceDs, stringfilePath = null)

        {

            if(string.IsNullOrEmpty(filePath))

            {

                filePath = Common.GetSaveFilePath();

            }

            if(string.IsNullOrEmpty(filePath)) returnnull;

            boolisCompatible = Common.GetIsCompatible(filePath);

            IWorkbook workbook = Common.CreateWorkbook(isCompatible);

            ICellStyle headerCellStyle = Common.GetCellStyle(workbook, true);

            //ICellStyle cellStyle = Common.GetCellStyle(workbook);

            for(inti = 0; i < sourceDs.Tables.Count; i++)

            {

                DataTable table = sourceDs.Tables[i];

                stringsheetName = string.IsNullOrEmpty(table.TableName) ? "result"+ i.ToString() : table.TableName;

                ISheet sheet = workbook.CreateSheet(sheetName);

                IRow headerRow = sheet.CreateRow(0);

                Dictionary<int, ICellStyle> colStyles = newDictionary<int, ICellStyle>();

                // handling header.

                foreach(DataColumn column intable.Columns)

                {

                    ICell headerCell = headerRow.CreateCell(column.Ordinal);

                    headerCell.SetCellValue(column.ColumnName);

                    headerCell.CellStyle = headerCellStyle;

                    sheet.AutoSizeColumn(headerCell.ColumnIndex);

                    colStyles[headerCell.ColumnIndex] = Common.GetCellStyle(workbook);

                }

                // handling value.

                introwIndex = 1;

                foreach(DataRow row intable.Rows)

                {

                    IRow dataRow = sheet.CreateRow(rowIndex);

                    foreach(DataColumn column intable.Columns)

                    {

                        ICell cell = dataRow.CreateCell(column.Ordinal);

                        //cell.SetCellValue((row[column] ?? "").ToString());

                        //cell.CellStyle = cellStyle;

                        Common.SetCellValue(cell, (row[column] ?? "").ToString(), column.DataType,colStyles);

                        Common.ReSizeColumnWidth(sheet, cell);

                    }

                    rowIndex++;

                }

                sheet.ForceFormulaRecalculation = true;

            }

            FileStream fs = newFileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            workbook.Write(fs);

            fs.Dispose();

            workbook = null;

            returnfilePath;

        }

        /// <summary>

        /// 由DataTable导出Excel

        /// </summary>

        /// <param name="sourceTable">要导出数据的DataTable</param>

        /// <param name="colAliasNames">导出的列名重命名数组</param>

        /// <param name="sheetName">工作薄名称,可选</param>

        /// <param name="filePath">导出路径,可选</param>

        /// <param name="colDataFormats">列格式化集合,可选</param>

        /// <returns></returns>

        publicstaticstringToExcel(DataTable sourceTable, string[] colAliasNames, stringsheetName = "result", stringfilePath = null, IDictionary<string, string> colDataFormats = null)

        {

            if(sourceTable.Rows.Count <= 0) returnnull;

            if(string.IsNullOrEmpty(filePath))

            {

                filePath = Common.GetSaveFilePath();

            }

            if(string.IsNullOrEmpty(filePath)) returnnull;

            if(colAliasNames == null|| sourceTable.Columns.Count != colAliasNames.Length)

            {

                thrownewArgumentException("列名重命名数组与DataTable列集合不匹配。", "colAliasNames");

            }

            boolisCompatible = Common.GetIsCompatible(filePath);

            IWorkbook workbook = Common.CreateWorkbook(isCompatible);

            ICellStyle headerCellStyle = Common.GetCellStyle(workbook, true);

            //ICellStyle cellStyle = Common.GetCellStyle(workbook);

            Dictionary<int, ICellStyle> colStyles = newDictionary<int, ICellStyle>();

            ISheet sheet = workbook.CreateSheet(sheetName);

            IRow headerRow = sheet.CreateRow(0);

            // handling header.

            foreach(DataColumn column insourceTable.Columns)

            {

                ICell headerCell = headerRow.CreateCell(column.Ordinal);

                headerCell.SetCellValue(colAliasNames[column.Ordinal]);

                headerCell.CellStyle = headerCellStyle;

                sheet.AutoSizeColumn(headerCell.ColumnIndex);

                if(colDataFormats != null&& colDataFormats.ContainsKey(column.ColumnName))

                {

                    colStyles[headerCell.ColumnIndex] = Common.GetCellStyleWithDataFormat(workbook, colDataFormats[column.ColumnName]);

                }

                else

                {

                    colStyles[headerCell.ColumnIndex] = Common.GetCellStyle(workbook);

                }

            }

            // handling value.

            introwIndex = 1;

            foreach(DataRow row insourceTable.Rows)

            {

                IRow dataRow = sheet.CreateRow(rowIndex);

                foreach(DataColumn column insourceTable.Columns)

                {

                    ICell cell = dataRow.CreateCell(column.Ordinal);

                    //cell.SetCellValue((row[column] ?? "").ToString());

                    //cell.CellStyle = cellStyle;

                    Common.SetCellValue(cell, (row[column] ?? "").ToString(), column.DataType, colStyles);

                    Common.ReSizeColumnWidth(sheet, cell);

                }

                rowIndex++;

            }

            sheet.ForceFormulaRecalculation = true;

            FileStream fs = newFileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            workbook.Write(fs);

            fs.Dispose();

            sheet = null;

            headerRow = null;

            workbook = null;

            returnfilePath;

        }

        /// <summary>

        /// 由DataGridView导出

        /// </summary>

        /// <param name="grid">要导出的DataGridView对象</param>

        /// <param name="sheetName">工作薄名称,可选</param>

        /// <param name="filePath">导出路径,可选</param>

        /// <param name="includeHiddenCol">导出时是否包含隐藏列,可选</param>

        /// <param name="colHeaderTexts">指定导出DataGridView的列标题名数组,可选</param>

        /// <param name="colDataFormats">列格式化集合,可选</param>

        /// <returns></returns>

        publicstaticstringToExcel(DataGridView grid, stringsheetName = "result", stringfilePath = null, boolincludeHiddenCol = false, string[] colHeaderTexts = null, IDictionary<string, string> colDataFormats = null)

        {

            if(grid.Rows.Count <= 0) returnnull;

            if(string.IsNullOrEmpty(filePath))

            {

                filePath = Common.GetSaveFilePath();

            }

            if(string.IsNullOrEmpty(filePath)) returnnull;

            boolisCompatible = Common.GetIsCompatible(filePath);

            DataGridViewColumn[] expCols = null;

            expCols = grid.Columns.Cast<DataGridViewColumn>().OrderBy(c => c.DisplayIndex).ToArray();

            if(!includeHiddenCol)

            {

                expCols = expCols.Where(c => c.Visible).ToArray();

            }

            if(colHeaderTexts != null&& colHeaderTexts.Length > 0)

            {

                expCols = expCols.Where(c => colHeaderTexts.Contains(c.HeaderText)).ToArray();

            }

            IWorkbook workbook = Common.CreateWorkbook(isCompatible);

            ICellStyle headerCellStyle = Common.GetCellStyle(workbook, true);

            //ICellStyle cellStyle = Common.GetCellStyle(workbook);

            ISheet sheet = workbook.CreateSheet(sheetName);

            IRow headerRow = sheet.CreateRow(0);

            Dictionary<int, ICellStyle> colStyles = newDictionary<int, ICellStyle>();

            for(inti = 0; i < expCols.Length; i++)

            {

                ICell headerCell = headerRow.CreateCell(i);

                headerCell.SetCellValue(expCols[i].HeaderText);

                headerCell.CellStyle = headerCellStyle;

                sheet.AutoSizeColumn(headerCell.ColumnIndex);

                if(colDataFormats != null&& colDataFormats.ContainsKey(expCols[i].HeaderText))

                {

                    colStyles[headerCell.ColumnIndex] = Common.GetCellStyleWithDataFormat(workbook, colDataFormats[expCols[i].HeaderText]);

                }

                else

                {

                    colStyles[headerCell.ColumnIndex] = Common.GetCellStyle(workbook);

                }

            }

            introwIndex = 1;

            foreach(DataGridViewRow row ingrid.Rows)

            {

                IRow dataRow = sheet.CreateRow(rowIndex);

                for(intn = 0; n < expCols.Length; n++)

                {

                    ICell cell = dataRow.CreateCell(n);

                    //cell.SetCellValue((row.Cells[expCols[n].Index].Value ?? "").ToString());

                    //cell.CellStyle = cellStyle;

                    Common.SetCellValue(cell, (row.Cells[expCols[n].Index].Value ?? "").ToString(), expCols[n].ValueType, colStyles);

                    Common.ReSizeColumnWidth(sheet, cell);

                }

                rowIndex++;

            }

            sheet.ForceFormulaRecalculation = true;

            FileStream fs = newFileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            workbook.Write(fs);

            fs.Dispose();

            sheet = null;

            headerRow = null;

            workbook = null;

            returnfilePath;

        }

        /// <summary>

        /// 由DataTable导出Excel

        /// </summary>

        /// <param name="sourceTable">要导出数据的DataTable</param>

        /// <param name="sheetName">工作薄名称,可选</param>

        /// <param name="filePath">导出路径,可选</param>

        /// <param name="colNames">需要导出的列名,可选</param>

        /// <param name="colAliasNames">导出的列名重命名,可选</param>

        /// <param name="colDataFormats">列格式化集合,可选</param>

        /// <returns></returns>

        publicstaticstringToExcel(DataTable sourceTable, stringsheetName = "result", stringfilePath = null, string[] colNames = null, IDictionary<string, string> colAliasNames = null, IDictionary<string, string> colDataFormats = null)

        {

            if(sourceTable.Rows.Count <= 0) returnnull;

            if(string.IsNullOrEmpty(filePath))

            {

                filePath = Common.GetSaveFilePath();

            }

            if(string.IsNullOrEmpty(filePath)) returnnull;

            boolisCompatible = Common.GetIsCompatible(filePath);

            IWorkbook workbook = Common.CreateWorkbook(isCompatible);

            ICellStyle headerCellStyle = Common.GetCellStyle(workbook, true);

            //ICellStyle cellStyle = Common.GetCellStyle(workbook);

            Dictionary<int, ICellStyle> colStyles = newDictionary<int, ICellStyle>();

            ISheet sheet = workbook.CreateSheet(sheetName);

            IRow headerRow = sheet.CreateRow(0);

            if(colNames == null|| colNames.Length <= 0)

            {

                colNames = sourceTable.Columns.Cast<DataColumn>().OrderBy(c => c.Ordinal).Select(c => c.ColumnName).ToArray();

            }

            // handling header.

            for(inti = 0; i < colNames.Length; i++)

            {

                ICell headerCell = headerRow.CreateCell(i);

                if(colAliasNames != null&& colAliasNames.ContainsKey(colNames[i]))

                {

                    headerCell.SetCellValue(colAliasNames[colNames[i]]);

                }

                else

                {

                    headerCell.SetCellValue(colNames[i]);

                }

                headerCell.CellStyle = headerCellStyle;

                sheet.AutoSizeColumn(headerCell.ColumnIndex);

                if(colDataFormats != null&& colDataFormats.ContainsKey(colNames[i]))

                {

                    colStyles[headerCell.ColumnIndex] = Common.GetCellStyleWithDataFormat(workbook, colDataFormats[colNames[i]]);

                }

                else

                {

                    colStyles[headerCell.ColumnIndex] = Common.GetCellStyle(workbook);

                }

            }

            // handling value.

            introwIndex = 1;

            foreach(DataRow row insourceTable.Rows)

            {

                IRow dataRow = sheet.CreateRow(rowIndex);

                for(inti = 0; i < colNames.Length; i++)

                {

                    ICell cell = dataRow.CreateCell(i);

                    //cell.SetCellValue((row[colNames[i]] ?? "").ToString());

                    //cell.CellStyle = cellStyle;

                    Common.SetCellValue(cell, (row[colNames[i]] ?? "").ToString(), sourceTable.Columns[colNames[i]].DataType,colStyles);

                    Common.ReSizeColumnWidth(sheet, cell);

                }

                rowIndex++;

            }

            sheet.ForceFormulaRecalculation = true;

            FileStream fs = newFileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            workbook.Write(fs);

            fs.Dispose();

            sheet = null;

            headerRow = null;

            workbook = null;

            returnfilePath;

        }

        /// <summary>

        ///由SheetFormatterContainer导出基于EXCEL模板的文件

        /// </summary>

        /// <param name="templatePath">模板路径</param>

        /// <param name="sheetName">模板中使用的工作薄名称</param>

        /// <param name="formatterContainer">模板数据格式化容器</param>

        /// <param name="filePath">导出路径,可选</param>

        /// <returns></returns>

        publicstaticstringToExcelWithTemplate<T>(stringtemplatePath, stringsheetName, SheetFormatterContainer<T> formatterContainer, stringfilePath = null)

        {

            if(!File.Exists(templatePath))

            {

                thrownewFileNotFoundException(templatePath + "文件不存在!");

            }

            if(string.IsNullOrEmpty(filePath))

            {

                filePath = Common.GetSaveFilePath();

            }

            if(string.IsNullOrEmpty(filePath)) returnnull;

            stringtemplateConfigFilePath = Common.GetTemplateConfigFilePath(templatePath, false);

            varworkbookParameterContainer = newWorkbookParameterContainer();

            workbookParameterContainer.Load(templateConfigFilePath);

            SheetParameterContainer sheetParameterContainer = workbookParameterContainer[sheetName];

            ExportHelper.ExportToLocal(templatePath, filePath, newSheetFormatter(sheetName, formatterContainer.GetFormatters(sheetParameterContainer)));

            returnfilePath;

        }

        /// <summary>

        /// 由SheetFormatterContainer集合导出基于EXCEL模板(多工作薄)的文件

        /// </summary>

        /// <param name="templatePath"></param>

        /// <param name="formatterContainers"></param>

        /// <param name="filePath"></param>

        /// <returns></returns>

        publicstaticstringToExcelWithTemplate(stringtemplatePath,IDictionary<string,SheetFormatterContainer<dynamic>> formatterContainers, stringfilePath = null)

        {

            if(!File.Exists(templatePath))

            {

                thrownewFileNotFoundException(templatePath + "文件不存在!");

            }

            if(string.IsNullOrEmpty(filePath))

            {

                filePath = Common.GetSaveFilePath();

            }

            if(string.IsNullOrEmpty(filePath)) returnnull;

            stringtemplateConfigFilePath = Common.GetTemplateConfigFilePath(templatePath, false);

            varworkbookParameterContainer = newWorkbookParameterContainer();

            workbookParameterContainer.Load(templateConfigFilePath);

            List<SheetFormatter> sheetFormatterList = newList<SheetFormatter>();

            foreach(varitem informatterContainers)

            {

                SheetParameterContainer sheetParameterContainer = workbookParameterContainer[item.Key];

                sheetFormatterList.Add(newSheetFormatter(item.Key, item.Value.GetFormatters(sheetParameterContainer)));

            }

            ExportHelper.ExportToLocal(templatePath, filePath,sheetFormatterList.ToArray());

            returnfilePath;

        }

    }

}

ExcelUtility.Base.Common:

+ View Code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

usingExcelReport;

usingNPOI.HSSF.UserModel;

usingNPOI.SS.UserModel;

usingNPOI.XSSF.UserModel;

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Data;

usingSystem.IO;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Text.RegularExpressions;

usingSystem.Windows.Forms;

namespaceExcelUtility.Base

{

    /// <summary>

    /// ExcelUtility类库内部通用功能类

    /// 作者:Zuowenjun

    /// 日期:2016/1/15

    /// </summary>

    internalstaticclassCommon

    {

        publicstaticstringDesktopDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

        /// <summary>

        /// 获取要保存的文件名称(含完整路径)

        /// </summary>

        /// <returns></returns>

        publicstaticstringGetSaveFilePath()

        {

            SaveFileDialog saveFileDig = newSaveFileDialog();

            saveFileDig.Filter = "Excel Office97-2003(*.xls)|*.xls|Excel Office2007及以上(*.xlsx)|*.xlsx";

            saveFileDig.FilterIndex = 0;

            saveFileDig.Title = "导出到";

            saveFileDig.OverwritePrompt = true;

            saveFileDig.InitialDirectory = DesktopDirectory;

            stringfilePath = null;

            if(saveFileDig.ShowDialog() == DialogResult.OK)

            {

                filePath = saveFileDig.FileName;

            }

            returnfilePath;

        }

        /// <summary>

        /// 获取要打开要导入的文件名称(含完整路径)

        /// </summary>

        /// <returns></returns>

        publicstaticstringGetOpenFilePath()

        {

            OpenFileDialog openFileDig = newOpenFileDialog();

            openFileDig.Filter = "Excel Office97-2003(*.xls)|*.xls|Excel Office2007及以上(*.xlsx)|*.xlsx";

            openFileDig.FilterIndex = 0;

            openFileDig.Title = "打开";

            openFileDig.CheckFileExists = true;

            openFileDig.CheckPathExists = true;

            openFileDig.InitialDirectory = Common.DesktopDirectory;

            stringfilePath = null;

            if(openFileDig.ShowDialog() == DialogResult.OK)

            {

                filePath = openFileDig.FileName;

            }

            returnfilePath;

        }

        /// <summary>

        /// 判断是否为兼容模式

        /// </summary>

        /// <param name="filePath"></param>

        /// <returns></returns>

        publicstaticboolGetIsCompatible(stringfilePath)

        {

            returnfilePath.EndsWith(".xls", StringComparison.OrdinalIgnoreCase);

        }

        /// <summary>

        /// 创建工作薄

        /// </summary>

        /// <param name="isCompatible"></param>

        /// <returns></returns>

        publicstaticIWorkbook CreateWorkbook(boolisCompatible)

        {

            if(isCompatible)

            {

                returnnewHSSFWorkbook();

            }

            else

            {

                returnnewXSSFWorkbook();

            }

        }

        /// <summary>

        /// 创建工作薄(依据文件流)

        /// </summary>

        /// <param name="isCompatible"></param>

        /// <param name="stream"></param>

        /// <returns></returns>

        publicstaticIWorkbook CreateWorkbook(boolisCompatible, dynamic stream)

        {

            if(isCompatible)

            {

                returnnewHSSFWorkbook(stream);

            }

            else

            {

                returnnewXSSFWorkbook(stream);

            }

        }

        /// <summary>

        /// 创建单元格样式

        /// </summary>

        /// <param name="sheet"></param>

        /// <returns></returns>

        publicstaticICellStyle GetCellStyle(IWorkbook workbook, boolisHeaderRow = false)

        {

            ICellStyle style = workbook.CreateCellStyle();

            if(isHeaderRow)

            {

                style.FillPattern = FillPattern.SolidForeground;

                style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;

                IFont f = workbook.CreateFont();

                f.Boldweight = (short)FontBoldWeight.Bold;

                style.SetFont(f);

            }

             

            style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;

            style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;

            style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;

            style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;

            returnstyle;

        }

        /// <summary>

        /// 根据单元格内容重新设置列宽

        /// </summary>

        /// <param name="sheet"></param>

        /// <param name="cell"></param>

        publicstaticvoidReSizeColumnWidth(ISheet sheet, ICell cell)

        {

            intcellLength = (Encoding.Default.GetBytes(cell.ToString()).Length + 2) * 256;

            constintmaxLength = 60 * 256; //255 * 256;

            if(cellLength > maxLength) //当单元格内容超过30个中文字符(英语60个字符)宽度,则强制换行

            {

                cellLength = maxLength;

                cell.CellStyle.WrapText = true;

            }

            intcolWidth = sheet.GetColumnWidth(cell.ColumnIndex);

            if(colWidth < cellLength)

            {

                sheet.SetColumnWidth(cell.ColumnIndex, cellLength);

            }

        }

        /// <summary>

        /// 创建单元格样式并设置数据格式化规则

        /// </summary>

        /// <param name="cell"></param>

        /// <param name="format"></param>

        publicstaticICellStyle GetCellStyleWithDataFormat(IWorkbook workbook, stringformat)

        {

            varstyle = GetCellStyle(workbook);

            vardataFormat = workbook.CreateDataFormat();

            shortformatId = -1;

            if(dataFormat isHSSFDataFormat)

            {

                formatId = HSSFDataFormat.GetBuiltinFormat(format);

            }

            if(formatId != -1)

            {

                style.DataFormat = formatId;

            }

            else

            {

                style.DataFormat = dataFormat.GetFormat(format);

            }

            returnstyle;

        }

        /// <summary>

        /// 依据值类型为单元格设置值

        /// </summary>

        /// <param name="cell"></param>

        /// <param name="value"></param>

        /// <param name="colType"></param>

        publicstaticvoidSetCellValue(ICell cell, stringvalue, Type colType, IDictionary<int, ICellStyle> colStyles)

        {

            stringdataFormatStr = null;

            switch(colType.ToString())

            {

                case"System.String": //字符串类型

                    cell.SetCellType(CellType.String);

                    cell.SetCellValue(value);

                    break;

                case"System.DateTime": //日期类型

                    DateTime dateV = newDateTime();

                    DateTime.TryParse(value, outdateV);

                    cell.SetCellValue(dateV);

                    dataFormatStr = "yyyy/mm/dd hh:mm:ss";

                    break;

                case"System.Boolean": //布尔型

                    boolboolV = false;

                    bool.TryParse(value, outboolV);

                    cell.SetCellType(CellType.Boolean);

                    cell.SetCellValue(boolV);

                    break;

                case"System.Int16": //整型

                case"System.Int32":

                case"System.Int64":

                case"System.Byte":

                    intintV = 0;

                    int.TryParse(value, outintV);

                    cell.SetCellType(CellType.Numeric);

                    cell.SetCellValue(intV);

                    dataFormatStr = "0";

                    break;

                case"System.Decimal": //浮点型

                case"System.Double":

                    doubledoubV = 0;

                    double.TryParse(value, outdoubV);

                    cell.SetCellType(CellType.Numeric);

                    cell.SetCellValue(doubV);

                    dataFormatStr = "0.00";

                    break;

                case"System.DBNull": //空值处理

                    cell.SetCellType(CellType.Blank);

                    cell.SetCellValue("");

                    break;

                default:

                    cell.SetCellType(CellType.Unknown);

                    cell.SetCellValue(value);

                    break;

            }

            if(!string.IsNullOrEmpty(dataFormatStr) && colStyles[cell.ColumnIndex].DataFormat <= 0) //没有设置,则采用默认类型格式

            {

                colStyles[cell.ColumnIndex] = GetCellStyleWithDataFormat(cell.Sheet.Workbook, dataFormatStr);

            }

            cell.CellStyle = colStyles[cell.ColumnIndex];

        }

        /// <summary>

        /// 从工作表中生成DataTable

        /// </summary>

        /// <param name="sheet"></param>

        /// <param name="headerRowIndex"></param>

        /// <returns></returns>

        publicstaticDataTable GetDataTableFromSheet(ISheet sheet, intheaderRowIndex)

        {

            DataTable table = newDataTable();

            IRow headerRow = sheet.GetRow(headerRowIndex);

            intcellCount = headerRow.LastCellNum;

            for(inti = headerRow.FirstCellNum; i < cellCount; i++)

            {

                if(headerRow.GetCell(i) == null|| headerRow.GetCell(i).StringCellValue.Trim() == "")

                {

                    // 如果遇到第一个空列,则不再继续向后读取

                    cellCount = i;

                    break;

                }

                DataColumn column = newDataColumn(headerRow.GetCell(i).StringCellValue);

                table.Columns.Add(column);

            }

            for(inti = (headerRowIndex + 1); i <= sheet.LastRowNum; i++)

            {

                IRow row = sheet.GetRow(i);

                //如果遇到某行的第一个单元格的值为空,则不再继续向下读取

                if(row != null&& !string.IsNullOrEmpty(row.GetCell(0).ToString()))

                {

                    DataRow dataRow = table.NewRow();

                    for(intj = row.FirstCellNum; j < cellCount; j++)

                    {

                        dataRow[j] = row.GetCell(j).ToString();

                    }

                    table.Rows.Add(dataRow);

                }

            }

            returntable;

        }

        /// <summary>

        /// 获取模板文件对应的模板格式配置XML文件路径(当不存在或较旧时,将会重新生成)

        /// </summary>

        /// <param name="templatePath"></param>

        /// <param name="newGenerate"></param>

        /// <returns></returns>

        publicstaticstringGetTemplateConfigFilePath(stringtemplatePath, boolnewGenerate = false)

        {

            stringtemplateConfigFilePath = Path.ChangeExtension(templatePath, ".xml");

            if(newGenerate || !File.Exists(templateConfigFilePath) || File.GetLastWriteTime(templatePath) > File.GetLastWriteTime(templateConfigFilePath))

            {

                WorkbookParameterContainer workbookParameter = ParseTemplate.Parse(templatePath);

                workbookParameter.Save(templateConfigFilePath);

            }

            returntemplateConfigFilePath;

        }

    }

}


第六个新增功能,支持模板中包含多个工作薄导出(目前这个方法还没有模拟测试,所以无法保证其一定有效,大家可以试试),该方法定义如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

/// <summary>

/// 由SheetFormatterContainer集合导出基于EXCEL模板(多工作薄)的文件

/// </summary>

/// <param name="templatePath"></param>

/// <param name="formatterContainers"></param>

/// <param name="filePath"></param>

/// <returns></returns>

public static string ToExcelWithTemplate(string templatePath,IDictionary<string,SheetFormatterContainer<dynamic>> formatterContainers, string filePath = null)

{

    if (!File.Exists(templatePath))

    {

        throw new FileNotFoundException(templatePath + "文件不存在!");

    }

    if (string.IsNullOrEmpty(filePath))

    {

        filePath = Common.GetSaveFilePath();

    }

    if (string.IsNullOrEmpty(filePath)) return null;

    string templateConfigFilePath = Common.GetTemplateConfigFilePath(templatePath, false);

    var workbookParameterContainer = new WorkbookParameterContainer();

    workbookParameterContainer.Load(templateConfigFilePath);

    List<SheetFormatter> sheetFormatterList = new List<SheetFormatter>();

    foreach (var item in formatterContainers)

    {

        SheetParameterContainer sheetParameterContainer = workbookParameterContainer[item.Key];

        sheetFormatterList.Add(new SheetFormatter(item.Key, item.Value.GetFormatters(sheetParameterContainer)));

    }

    ExportHelper.ExportToLocal(templatePath, filePath,sheetFormatterList.ToArray());

    return filePath;

}

该类库源码已分享到该路径中:http://git.oschina.net/zuowj/ExcelUtility    GIT Repository路径:[email protected]:zuowj/ExcelUtility.git   (会持续更新及修正BUG)

猜你喜欢

转载自blog.csdn.net/Andrewniu/article/details/89647172