深度优先

这个家伙好懒,除了文章什么都没留下

0%

读取word —>填充数据 —>保存word

SpireWordHelper:

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
public class SpireWordHelper
{
public void CreateWord(CreatePaymentData data)
{
Document doc = new Document(data.TempWord);
doc.Properties.FormFieldShading = false;
var sections = doc.Sections[0];
var paraList = sections.Paragraphs;
paraList[2].Text = "至:" + data.sName;
paraList[17].Text = "至:" + data.sName;

var nowDate = DateTime.Now;

paraList[13].Text = nowDate.ToString("yyyy年MM月");
paraList[28].Text = nowDate.ToString("yyyy年MM月");

Table table1 = doc.Sections[0].Tables[0] as Table;
Table table2 = doc.Sections[0].Tables[1] as Table;
var printer = data.BillingPrinters;
decimal serviceTotal = 0, fixedTotal = 0;
for (int i = 0; i < printer.Count; i++)
{
table1.AddRow();
table2.AddRow();
table1 = GetRange(table1, printer[i].AssetModel, i + 1, 0);
table2 = GetRange(table2, printer[i].AssetModel, i + 1, 0);

table1 = GetRange(table1, printer[i].SerialNo, i + 1, 1);
table2 = GetRange(table2, printer[i].SerialNo, i + 1, 1);

table1 = GetRange(table1, printer[i].Address, i + 1, 2);
table2 = GetRange(table2, printer[i].Address, i + 1, 2);

table1 = GetRange(table1, "XXX", i + 1, 3);
table2 = GetRange(table2, "XXX", i + 1, 3);

table1 = GetRange(table1, printer[i].ServicePeriod1, i + 1, 4, 0);
table1 = GetRange(table1, printer[i].ServicePeriod2, i + 1, 4, 1);

table2 = GetRange(table2, printer[i].ServicePeriod1, i + 1, 4, 0);
table2 = GetRange(table2, printer[i].ServicePeriod2, i + 1, 4, 1);

table1 = GetRange(table1,printer[i].ServiceTotalAmount.ToString(), i + 1, 5);
table2 = GetRange(table2, printer[i].FixedTotalAmount.ToString(), i + 1, 5);
serviceTotal += printer[i].ServiceTotalAmount;
fixedTotal += printer[i].FixedTotalAmount;
}
//汇总数据
table1.AddRow();
table1 = GetRange(table1, "XXXX", printer.Count + 1, 4);
table1 = GetRange(table1, serviceTotal.ToString(), printer.Count + 1, 5);
table2.AddRow();
table2 = GetRange(table2, "XXXX", printer.Count + 1, 4);
table2 = GetRange(table2, fixedTotal.ToString(), printer.Count + 1, 5);

string filePath = data.SavePath + "/" + data.FileNameWord;
doc.SaveToFile(filePath, FileFormat.Docx);
WordHelper wordHelper = new WordHelper();
wordHelper.RemoveWatermark(filePath);
}

private Table GetRange(Table table, string text, int i, int j, int k = 0)
{
var range = table[i, j].AddParagraph().AppendText(text);
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 9;
range.CharacterFormat.TextColor = Color.Black;
range.CharacterFormat.Bold = false;
table[i, j].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
table[i, j].Paragraphs[k].Format.HorizontalAlignment = HorizontalAlignment.Center;
return table;
}
}

NPOI版:

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
public bool CreatePayment(CreatePaymentData data)
{
using (FileStream stream = File.OpenRead(data.TempPlatePath))
{
XWPFDocument doc = new XWPFDocument(stream);
var paraList = doc.Paragraphs;

var toTitle1 = paraList[2].Runs[1];
toTitle1.SetText(data.sName);

var toTitle2 = paraList[17].Runs[1];
toTitle2.SetText(data.sName);

var nowDate = DateTime.Now;
var year1 = paraList[13].Runs[0];
year1.SetText(nowDate.ToString("yyyy"));
var month1 = paraList[13].Runs[2];
month1.SetText(nowDate.ToString("MM"));

var year2 = paraList[28].Runs[0];
year2.SetText(nowDate.ToString("yyyy"));
var month2 = paraList[28].Runs[2];
month2.SetText(nowDate.ToString("MM"));

#region 填充table1中的数据
var table = doc.Tables[0];
//table.RemoveRow(0);
for (int i = 0; i < 5; i++)
{
CT_Row m_NewRow = new CT_Row();
XWPFTableRow m_Row = new XWPFTableRow(m_NewRow, table);

var c0 = m_Row.CreateCell();
c0.SetBorderBottom(XWPFTable.XWPFBorderType.THICK, 1, 4, "#000000");
c0.SetParagraph(SetCellText(table, JoinN(data.AssetModel + i)));

var c1 = m_Row.CreateCell();
c1.SetParagraph(SetCellText(table, JoinN(data.SerialNo)));

var c2 = m_Row.CreateCell();
c2.SetParagraph(SetCellText(table, JoinN(data.Address)));

var c3 = m_Row.CreateCell();
c3.SetParagraph(SetCellText(table, "XXXX"));

var c4 = m_Row.CreateCell();
c4.SetParagraph(SetCellText(table, data.ServicePeriod1 + "\n" + data.ServicePeriod2));

var c5 = m_Row.CreateCell();
c5.SetParagraph(SetCellText(table, JoinN("1234")));

table.AddRow(m_Row);
}
#endregion
if (!Directory.Exists(data.SavePath))
{
Directory.CreateDirectory(data.SavePath);
}

FileStream outFile = new FileStream(data.SavePath + "/" + data.FileName, FileMode.Create);
doc.Write(outFile);
outFile.Close();
return true;
}
}

private string JoinN(string address)
{
for (int i = 10; i < address.Length; i+=10)
{
address = address.Insert(i,"\n");
}
return address;
}

/// <summary>
/// 设置字体格式
/// </summary>
/// <param name="doc"></param>
/// <param name="table"></param>
/// <param name="setText"></param>
/// <returns></returns>
public XWPFParagraph SetCellText(XWPFTable table, string setText)
{
CT_P para = new CT_P();
XWPFParagraph pCell = new XWPFParagraph(para, table.Body);
pCell.Alignment = ParagraphAlignment.CENTER;//字体居中
pCell.VerticalAlignment = TextAlignment.CENTER;//字体居中
pCell.BorderLeft = Borders.Thick;
pCell.BorderRight = Borders.Thick;
pCell.BorderTop = Borders.Thick;
pCell.BorderBottom = Borders.Thick;
XWPFRun r1c1 = pCell.CreateRun();
r1c1.SetText(setText);
r1c1.FontSize = 10;
//r1c1.SetFontFamily("华文楷体", FontCharRange.None);//设置雅黑字体
r1c1.SetTextPosition(15);//设置高度
return pCell;
}

public void RemoveWatermark(string filePath)
{
using (FileStream stream = File.OpenRead(filePath))
{
XWPFDocument doc = new XWPFDocument(stream);
var paraList = doc.Paragraphs;
paraList[0].RemoveRun(0);

FileStream outFile = new FileStream(filePath, FileMode.Create);
doc.Write(outFile);
outFile.Close();
}
}
}

最近项目中有许多地方需要将数据导出成Excel文件,因此写了个公共模块做Excel的导出!

代码:

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
/// <summary>
/// 将IList对象导出Excel
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="columnTitle"></param>
/// <param name="title"></param>
/// <param name="path"></param>
/// <returns></returns>
public string DataToExcel<T>(IList<T> list, List<DataKeyValue> columnTitle, string title,string path)
where T : class
{
DataTable dt = ListToDataTable(list);
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.CreateSheet("Sheet1") as HSSFSheet;

HSSFRow dataRow = sheet.CreateRow(0) as HSSFRow;
dataRow = sheet.CreateRow(0) as HSSFRow;
CellRangeAddress region = new CellRangeAddress(0, 0, 0, columnTitle.Count - 1);
sheet.AddMergedRegion(region);
IRow hrow = sheet.CreateRow(0);
hrow.Height = 20 * 20;
ICell icellltop0 = hrow.CreateCell(0);

IFont font12 = book.CreateFont();
font12.FontHeightInPoints = 12;
font12.FontName = "微软雅黑";
font12.Boldweight = short.MaxValue;
font12.Color = Black.Index;

ICellStyle cellStyle = book.CreateCellStyle();
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
cellStyle.BorderBottom = BorderStyle.Thin;
cellStyle.BorderLeft = BorderStyle.Thin;
cellStyle.BorderRight = BorderStyle.Thin;
cellStyle.BorderTop = BorderStyle.Thin;
for (int i = region.FirstRow; i <= region.LastRow; i++)
{
IRow row = HSSFCellUtil.GetRow(i, sheet);
for (int j = region.FirstColumn; j <= region.LastColumn; j++)
{
ICell singleCell = HSSFCellUtil.GetCell(row, (short)j);
singleCell.CellStyle = cellStyle;
}
}

cellStyle.SetFont(font12);
cellStyle.FillBackgroundColor = BlueGrey.Index;
icellltop0.CellStyle = cellStyle;
icellltop0.SetCellValue(title + "_" + DateTime.Now.ToString("yyyy-MM-dd"));

ICellStyle TitleStyle = book.CreateCellStyle();
TitleStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.PaleBlue.Index;
TitleStyle.FillPattern = FillPattern.SolidForeground;
TitleStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
TitleStyle.BorderBottom = BorderStyle.Thin;
TitleStyle.BorderLeft = BorderStyle.Thin;
TitleStyle.BorderRight = BorderStyle.Thin;
TitleStyle.BorderTop = BorderStyle.Thin;

IRow TitleRow = sheet.CreateRow(1);
TitleRow.Height = 20 * 15;
for (int i = 0; i < columnTitle.Count; i++)
{
ICell cell = TitleRow.CreateCell(i);
cell.CellStyle = TitleStyle;
cell.SetCellValue(columnTitle[i].Value);
sheet.SetColumnWidth(i, columnTitle[i].ColumnWidth);
}

// data
for (int i = 0; i < dt.Rows.Count; i++)
{
IRow row = sheet.CreateRow(i + 2);
for (int j = 0; j < dt.Columns.Count; j++)
{
}
for (int j = 0; j < columnTitle.Count; j++)
{
row.CreateCell(j).SetCellValue(dt.Rows[i][columnTitle[j].key].ToString());
}
}

ICellStyle borStyle = book.CreateCellStyle();
borStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
borStyle.BorderBottom = BorderStyle.Thin;
borStyle.BorderLeft = BorderStyle.Thin;
borStyle.BorderRight = BorderStyle.Thin;
borStyle.BorderTop = BorderStyle.Thin;

for (int i = 1; i <= dt.Rows.Count+1; i++)
{
IRow row = HSSFCellUtil.GetRow(i, sheet);
row.Height = 20 * 16;
for (int j = 0; j < columnTitle.Count; j++)
{
ICell singleCell = HSSFCellUtil.GetCell(row, (short)j);
singleCell.CellStyle = borStyle;
}
}
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string fileName = title + DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls";
using (FileStream fsWrite = File.OpenWrite(path + fileName))
{
book.Write(fsWrite);
}
return fileName;
}

IList To DataTable:

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
/// <summary>
/// 实体列表转换成DataTable
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="list"> 实体列表</param>
/// <returns></returns>
public DataTable ListToDataTable<T>(IList<T> list)
where T : class
{
if (list == null || list.Count <= 0)
{
return null;
}
DataTable dt = new DataTable(typeof(T).Name);
DataColumn column;
DataRow row;

PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

int length = myPropertyInfo.Length;
bool createColumn = true;

foreach (T t in list)
{
if (t == null)
{
continue;
}
row = dt.NewRow();
for (int i = 0; i < length; i++)
{
PropertyInfo pi = myPropertyInfo[i];
string name = pi.Name;
if (createColumn)
{
column = new DataColumn(name, typeof(string));
dt.Columns.Add(column);
}
row[name] = pi.GetValue(t,null) is null ?"": pi.GetValue(t, null).ToString();
}
if (createColumn)
{
createColumn = false;
}
dt.Rows.Add(row);
}
return dt;
}

配置列信息:

1
2
3
4
5
6
public class DataKeyValue
{
public string key { get; set; }
public string Value { get; set; }
public int ColumnWidth { get; set; }
}

调用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ExcelHelper excelHelper = new ExcelHelper();
List<DataKeyValue> list = new List<DataKeyValue>() {
new DataKeyValue(){key="ID",Value="编号",ColumnWidth=256*10},
new DataKeyValue(){key="XXX",Value="XXX",ColumnWidth=256*20},
new DataKeyValue(){key="XXX",Value="XXX",ColumnWidth=256*20},
new DataKeyValue(){key="XXX",Value="XXX",ColumnWidth=256*10},
new DataKeyValue(){key="XXX",Value="XXX",ColumnWidth=256*35},

new DataKeyValue(){key="XXX",Value="XXX",ColumnWidth=256*45},
new DataKeyValue(){key="XXX",Value="XXX",ColumnWidth=256*45},
new DataKeyValue(){key="XXX",Value="XXX",ColumnWidth=256*45},
new DataKeyValue(){key="XXX",Value="XXX",ColumnWidth=256*45},
};
string path = ConfigurationManager.AppSettings["ExportPath"].ToString();
string result = excelHelper.DataToExcel(allCustomerList, list, "Customer_", path);

似乎WeChat目前也还没有提供小程序直接分享到朋友圈的API接口。

如果想要分享到朋友圈只能间接的通过图片,识别二维码的方式,有点繁琐。

效果图:

share.wxml 代码:

1
2
3
4
5
<!-- canvas绘制分享图 -->
<view class='body'>
<canvas canvas-id="myCanvas" class='canvas-box'></canvas>
<button bindtap="ShowCanvas">Show</button>
</view>

share.wxss 代码:

1
2
3
4
5
6
7
8
9
10
11
.canvas-box {
width: 620rpx;
height: 990rpx;
margin: 0 auto;
border-radius: 20rpx;
}

.body {
background-color: rgba(0, 0, 0, 0.5);
}

share.js 代码:

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
// pages/share/share.js
Page({
data: {
avatarUrl: 'http://120.77.181.53/img/avatarUrl.jpg',
qrcode: 'http://120.77.181.53/img/qrcode.png',
qrcode_temp: null,
windowWidth: 0,
scale: 1.38,
},
onLoad: function (options) {
this.setData({
windowWidth: wx.getSystemInfoSync().windowWidth * 0.825
})
var that = this;
wx.downloadFile({
url: that.data.avatarUrl,
success: function (res1) {
//缓存头像图片
that.setData({
portrait_temp: res1.tempFilePath
})
//缓存canvas绘制小程序二维码
wx.downloadFile({
url: that.data.qrcode,
success: function (res2) {
console.log('二维码:' + res2.tempFilePath)
//缓存二维码
that.setData({
qrcode_temp: res2.tempFilePath
})
console.log('开始绘制图片');
that.drawImage();
}
})
}
})
},
canvasToImage: function () {
var that = this
wx.canvasToTempFilePath({
x: 0,
y: 0,
width: that.data.windowWidth,
height: that.data.windowWidth * that.data.scale,
destWidth: that.data.windowWidth * 4,
destHeight: that.data.windowWidth * 4 * that.data.scale,
canvasId: 'myCanvas',
success: function (res) {
console.log('朋友圈分享图生成成功:' + res.tempFilePath)
wx.previewImage({
current: res.tempFilePath, // 当前显示图片的http链接
urls: [res.tempFilePath] // 需要预览的图片http链接列表
})
},
fail: function (err) {
console.log('失败')
console.log(err)
}
})
},
drawImage: function () {
//绘制canvas图片
var that = this
const ctx = wx.createCanvasContext('myCanvas')
var bgPath = '../../img/card_bg.jpg'
var portraitPath = that.data.portrait_temp
var hostNickname = 'Guan Xing'

var qrPath = that.data.qrcode_temp
var windowWidth = that.data.windowWidth
that.setData({
scale: 1.6
})
//绘制背景图片
ctx.drawImage(bgPath, 0, 0, windowWidth, that.data.scale * windowWidth)

//绘制头像
ctx.save();
ctx.beginPath();
ctx.arc(windowWidth / 2, 0.32 * windowWidth, 0.15 * windowWidth, 0, 2 * Math.PI);
ctx.clip();
ctx.drawImage(portraitPath, 0.7 * windowWidth / 2, 0.17 * windowWidth, 0.3 * windowWidth, 0.3 * windowWidth);
ctx.restore();
//绘制第一段文本
ctx.setFillStyle('#000');
ctx.setFontSize(0.037 * windowWidth);
ctx.setTextAlign('center');
ctx.fillText(hostNickname + ' 正在参加疯狂红包活动', windowWidth / 2, 0.70 * windowWidth);
//绘制第二段文本
ctx.setFillStyle('#000');
ctx.setFontSize(0.037 * windowWidth);
ctx.setTextAlign('center');
ctx.fillText('邀请你一起来领券抢红包啦~', windowWidth / 2, 0.76 * windowWidth);
//绘制二维码
ctx.drawImage(qrPath, 0.64 * windowWidth / 2, 0.90 * windowWidth, 0.36 * windowWidth, 0.36 * windowWidth);
//绘制第三段文本
ctx.setFillStyle('#000');
ctx.setFontSize(0.037 * windowWidth);
ctx.setTextAlign('center');
ctx.fillText('长按二维码保存至手机', windowWidth / 2, 1.42 * windowWidth);

ctx.lineWidth = 2;
ctx.strokeStyle = "#070616";
ctx.strokeRect(0, 0, windowWidth, that.data.scale * windowWidth);
ctx.draw();
},
ShowCanvas: function (event) {
wx.hideLoading();
this.canvasToImage();
},
})