這里介紹一個很方便實用的庫:Magicodes.IE,導入導出通用庫,通過導入導出DTO模型來控制導入和導出,支持Excel、Word、Pdf和Html。
本篇介紹基本使用方法,使用方法非常簡單,不需要學習npoi的應用。
在vs中,新建一個控制台項目(測試用,可以建其他項目,比如asp.net mvc ,winform都可以)
右鍵單擊項目中的引用,選擇nuget包管理,導入nuget包:這里只使用excel,所以只導入了Magicodes.IE.Excel
-
Magicodes .IE .Core
-
Magicodes .IE .Excel
-
Magicodes .IE .Pdf
-
Magicodes .IE .Word
-
Magicodes .IE .Html
1. 首先准備好一個類,這個類包含需要導出的屬性一、導出數據
-
public class Question
-
{
-
public string Title { get; set; }
-
public string Content { get; set; }
-
public string Options { get; set; }
-
public string Answer { get; set; }
-
}
2.導出方法
這里如果new List<Question>()不設置數據,就可以直接導出Question的模板
-
static void ExportQuestion()
-
{
-
//這里需要補充,如果路徑不存在要創建路徑,否則會報錯
-
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "question.xlsx");
-
var exporter = new ExcelExporter();
-
var result = exporter.Export(filePath, new List<Question>() {
-
new Question()
-
{
-
Title = "Question1",
-
Content = "Question content 1",
-
Options = "A:option1,B:option2,C:option3,D:option4",
-
Answer = "A",
-
},
-
new Question()
-
{
-
Title = "Question2",
-
Content = "Question content 2",
-
Options = "A:option11,B:option22,C:option33,D:option43",
-
Answer = "B",
-
}
-
});
-
}
其他方法參考下代碼吧:
-
using Magicodes.ExporterAndImporter.Core;
-
using Magicodes.ExporterAndImporter.Excel;
-
using Magicodes.ExporterAndImporter.Excel.Builder;
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel.DataAnnotations;
-
using System.IO;
-
using System.Text;
-
using System.Threading.Tasks;
-
-
namespace Test
-
{
-
class Program
-
{
-
//static void Main(string[] args)
-
static async Task Main(string[] args)
-
{
-
await Import3();
-
Console.WriteLine( "Hello World!");
-
}
-
-
-
-
/// <summary>
-
/// 導出excel測試:excel1
-
/// </summary>
-
static void Demo1()
-
{
-
//這里需要補充,如果路徑不存在要創建路徑,否則會報錯
-
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "demo1.xlsx");
-
var exporter = new ExcelExporter();
-
var result = exporter.Export(filePath, new List<ExportTestData>() {
-
new ExportTestData()
-
{
-
Name1 = "1",
-
Name2 = "test",
-
Name3 = "12",
-
Name4 = "11",
-
},
-
new ExportTestData()
-
{
-
Name1 = "1",
-
Name2 = "test",
-
Name3 = "12",
-
Name4 = "11",
-
}
-
});
-
}
-
-
static void ExportQuestion()
-
{
-
//這里需要補充,如果路徑不存在要創建路徑,否則會報錯
-
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "question.xlsx");
-
var exporter = new ExcelExporter();
-
var result = exporter.Export(filePath, new List<Question>() {
-
new Question()
-
{
-
Title = "Question1",
-
Content = "Question content 1",
-
Options = "A:option1,B:option2,C:option3,D:option4",
-
Answer = "A",
-
},
-
new Question()
-
{
-
Title = "Question2",
-
Content = "Question content 2",
-
Options = "A:option11,B:option22,C:option33,D:option43",
-
Answer = "B",
-
}
-
});
-
}
-
-
/// <summary>
-
/// 導出空模板測試:excel11
-
/// </summary>
-
static void Demo11()
-
{
-
//這里需要補充,如果路徑不存在要創建路徑,否則會報錯
-
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ImportProduct2Dto.xlsx");
-
var exporter = new ExcelExporter();
-
var result = exporter.Export(filePath, new List<ImportProduct2Dto>());
-
}
-
-
-
/// <summary>
-
/// 利用特性導出excel2
-
/// </summary>
-
static void Demo2()
-
{
-
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "demo2.xlsx");
-
ExcelExporter exporter = new ExcelExporter();
-
var result = exporter.Export(filePath, new List<ExportTestDataWithAttrs>()
-
{
-
new ExportTestDataWithAttrs()
-
{
-
Text1 = "啊實打實大蘇打撒",
-
Name= "aa",
-
Number = 5000,
-
Text2 = "w薩達薩達薩達撒",
-
Text3 = "sadsad打發打發士大夫的"
-
},
-
new ExportTestDataWithAttrs()
-
{
-
Text1 = "啊實打實大蘇打撒",
-
Name= "啊實打實大蘇打撒",
-
Number = 6000,
-
Text2 = "w薩達薩達薩達撒",
-
Text3 = "sadsad打發打發士大夫的"
-
},
-
new ExportTestDataWithAttrs()
-
{
-
Text1 = "啊實打實速度大蘇打撒",
-
Name= "薩達薩達",
-
Number = 6000,
-
Text2 = "突然他也讓他人",
-
Text3 = "sadsad打發打發士大夫的"
-
},
-
});
-
}
-
-
/// <summary>
-
/// 列頭處理或者多語言支持測試
-
/// </summary>
-
static void Demo3()
-
{
-
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "testAttrsLocalization.xlsx");
-
if (File.Exists(filePath)) File.Delete(filePath);
-
var exporter = new ExcelExporter();
-
ExcelBuilder.Create().WithColumnHeaderStringFunc((key) =>
-
{
-
if (key.Contains( "文本"))
-
{
-
return "Text";
-
}
-
return "未知語言";
-
}).Build();
-
var result = exporter.Export(filePath, new List<AttrsLocalizationTestData>()
-
{
-
new AttrsLocalizationTestData()
-
{
-
Text = "啊實打實大蘇打撒",
-
Name= "aa",
-
Number = 5000,
-
Text2 = "w薩達薩達薩達撒",
-
Text3 = "sadsad打發打發士大夫的"
-
},
-
new AttrsLocalizationTestData()
-
{
-
Text = "啊實打實大蘇打撒",
-
Name= "啊實打實大蘇打撒",
-
Number = 6000,
-
Text2 = "w薩達薩達薩達撒",
-
Text3 = "sadsad打發打發士大夫的"
-
},
-
new AttrsLocalizationTestData()
-
{
-
Text = "啊實打實速度大蘇打撒",
-
Name= "薩達薩達",
-
Number = 6000,
-
Text2 = "突然他也讓他人",
-
Text3 = "sadsad打發打發士大夫的"
-
},
-
});
-
}
-
-
-
-
/// <summary>
-
/// 從excel導入數據測試1
-
/// </summary>
-
/// <returns></returns>
-
private static async Task Import1()
-
{
-
var importer = new ExcelImporter();
-
var importResult = await importer.Import<ImportProductDto>(Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "importer1.xlsx"));
-
foreach ( var item in importResult.Data)
-
{
-
Console.WriteLine(item.Name+ "-"+item.Code+ "-"+item.BarCode);
-
}
-
}
-
-
/// <summary>
-
/// 導入帶有枚舉值的數據
-
/// </summary>
-
/// <returns></returns>
-
private static async Task Import2()
-
{
-
var importer = new ExcelImporter();
-
var importResult = await importer.Import<ImportProduct2Dto>(Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ImportProduct2Dto.xlsx"));
-
foreach ( var item in importResult.Data)
-
{
-
Console.WriteLine(item.Name + "-" + item.Code + "-" + item.BarCode+ "-"+item.Type.ToString());
-
}
-
}
-
-
/// <summary>
-
/// 導入數據驗證
-
/// </summary>
-
/// <returns></returns>
-
private static async Task Import3()
-
{
-
var importer = new ExcelImporter();
-
var importResult = await importer.Import<ImportProduct3Dto>(Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ImportProduct2Dto.xlsx"));
-
if (importResult.HasError)
-
{
-
StringBuilder stringBuilder = new StringBuilder();
-
foreach ( var item in importResult.RowErrors)
-
{
-
stringBuilder.AppendLine( "出錯行數:" + item.RowIndex);
-
foreach ( var fielderror in item.FieldErrors)
-
{
-
stringBuilder.AppendLine( "\t出錯列:" + fielderror.Key+ "\n\t出錯原因:"+fielderror.Value);
-
}
-
}
-
Console.WriteLine(stringBuilder.ToString());
-
}
-
else
-
{
-
foreach ( var item in importResult.Data)
-
{
-
Console.WriteLine(item.Name + "-" + item.Code + "-" + item.BarCode + "-" + item.Type.ToString());
-
}
-
}
-
-
}
-
-
-
}
-
-
-
public class ExportTestData
-
{
-
public string Name1 { get; set; }
-
public string Name2 { get; set; }
-
public string Name3 { get; set; }
-
public string Name4 { get; set; }
-
}
-
-
public class Question
-
{
-
public string Title { get; set; }
-
public string Content { get; set; }
-
public string Options { get; set; }
-
public string Answer { get; set; }
-
}
-
-
/// <summary>
-
/// 特性導出Excel
-
/// </summary>
-
[]
-
public class ExportTestDataWithAttrs
-
{
-
[]
-
public string Text1 { get; set; }
-
-
[]
-
public string Text2 { get; set; }
-
-
[]
-
public string Text3 { get; set; }
-
-
[]
-
public double Number { get; set; }
-
-
[]
-
public string Name { get; set; }
-
-
}
-
-
/// <summary>
-
/// 列頭處理或者多語言支持
-
/// </summary>
-
[]
-
public class AttrsLocalizationTestData
-
{
-
[]
-
public string Text { get; set; }
-
-
[]
-
public string Text2 { get; set; }
-
-
[]
-
public string Text3 { get; set; }
-
-
[]
-
public double Number { get; set; }
-
-
[]
-
public string Name { get; set; }
-
}
-
-
-
-
/// <summary>
-
/// 從excel導入數據 dto
-
/// </summary>
-
public class ImportProductDto
-
{
-
/// <summary>
-
/// 產品名稱
-
/// </summary>
-
[]
-
public string Name { get; set; }
-
/// <summary>
-
/// 產品代碼
-
/// </summary>
-
[]
-
public string Code { get; set; }
-
/// <summary>
-
/// 產品條碼
-
/// </summary>
-
[]
-
public string BarCode { get; set; }
-
}
-
-
[]
-
public class ImportProduct2Dto
-
{
-
/// <summary>
-
/// 產品名稱
-
/// </summary>
-
[]
-
[]
-
public string Name { get; set; }
-
/// <summary>
-
/// 產品代碼
-
/// </summary>
-
[]
-
[]
-
public string Code { get; set; }
-
/// <summary>
-
/// 產品條碼
-
/// </summary>
-
[]
-
[]
-
public string BarCode { get; set; }
-
/// <summary>
-
/// 客戶Id
-
/// </summary>
-
[]
-
[]
-
public long ClientId { get; set; }
-
/// <summary>
-
/// 產品型號
-
/// </summary>
-
[]
-
[]
-
public string Model { get; set; }
-
/// <summary>
-
/// 申報價值
-
/// </summary>
-
[]
-
[]
-
public double DeclareValue { get; set; }
-
/// <summary>
-
/// 貨幣單位
-
/// </summary>
-
[]
-
[]
-
public string CurrencyUnit { get; set; }
-
/// <summary>
-
/// 品牌名稱
-
/// </summary>
-
[]
-
[]
-
public string BrandName { get; set; }
-
/// <summary>
-
/// 尺寸
-
/// </summary>
-
[]
-
[]
-
public string Size { get; set; }
-
/// <summary>
-
/// 重量
-
/// </summary>
-
[]
-
[]
-
public double Weight { get; set; }
-
-
/// <summary>
-
/// 類型
-
/// </summary>
-
[]
-
[]
-
public ImporterProductType Type { get; set; }
-
-
/// <summary>
-
/// 是否行
-
/// </summary>
-
[]
-
[]
-
public bool IsOk { get; set; }
-
}
-
-
-
public class ImportProduct3Dto
-
{
-
/// <summary>
-
/// 產品名稱
-
/// </summary>
-
[]
-
[]
-
public string Name { get; set; }
-
/// <summary>
-
/// 產品代碼
-
/// </summary>
-
[]
-
[]
-
public string Code { get; set; }
-
/// <summary>
-
/// 產品條碼
-
/// </summary>
-
[]
-
[]
-
[]
-
public string BarCode { get; set; }
-
/// <summary>
-
/// 客戶Id
-
/// </summary>
-
[]
-
public long ClientId { get; set; }
-
/// <summary>
-
/// 產品型號
-
/// </summary>
-
[]
-
public string Model { get; set; }
-
/// <summary>
-
/// 申報價值
-
/// </summary>
-
[]
-
public double DeclareValue { get; set; }
-
/// <summary>
-
/// 貨幣單位
-
/// </summary>
-
[]
-
public string CurrencyUnit { get; set; }
-
/// <summary>
-
/// 品牌名稱
-
/// </summary>
-
[]
-
public string BrandName { get; set; }
-
/// <summary>
-
/// 尺寸
-
/// </summary>
-
[]
-
public string Size { get; set; }
-
/// <summary>
-
/// 重量
-
/// </summary>
-
[]
-
public double Weight { get; set; }
-
-
/// <summary>
-
/// 類型
-
/// </summary>
-
[]
-
public ImporterProductType Type { get; set; }
-
-
/// <summary>
-
/// 是否行
-
/// </summary>
-
[]
-
public bool IsOk { get; set; }
-
}
-
-
public enum ImporterProductType
-
{
-
[]
-
One,
-
[]
-
Two
-
}
-
}