在从DataSet导出Txt一篇中,我们还有一个问题没有解决:每个Txt的总金额不能超过10万,单笔金额不能超过5万(上一篇中为5000)。
按照上一篇中我提到的解决方案,首先在服务器中生成Txt,再打包下载到客户端。
首先,我们需要两个方法:一个是创建文件的方法,一个是压缩文件的方法,下面我给出具体的代码。
创建Txt文件的方法:
/// <summary>
/// 创建Txt文档
/// </summary>
/// <param name="str">写入Txt的内容</param>
/// <param name="list">生成的Txt的路径列表</param>
private static void CreateTxtFile(string str, List<string> list)
{
if (str.Length == 0)
{
return;
}
string fileName = Guid.NewGuid() + ".txt";
string path = @"F:\\Txt\\" + fileName;
list.Add(path);
StreamWriter sr = File.CreateText(path);
sr.Write(str);
sr.Close();
}
压缩文件的方法:
需要引用ICSharpCode.SharpZipLib.dll
public static void CompressFile(string savePath, string txtName, List<string> fileNames)
{
ZipOutputStream stream = new ZipOutputStream(File.Create(savePath));
//设置压缩级别(0-9),值越大压缩越厉害
stream.SetLevel(8);
//遍历所有要压缩的文件
for (int i = 0; i < fileNames.Count; i++)
{
if (File.Exists(fileNames[i]))
{
FileStream fs = File.OpenRead(fileNames[i]);
byte[] buffer = new byte[fs.Length];//设置缓冲区大小
int size = fs.Read(buffer, 0, buffer.Length); //读入缓冲区中的字节数
//string entryName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
string entryName = txtName + "(" + (i + 1) + ").txt";
ZipEntry entry = new ZipEntry(entryName);
stream.PutNextEntry(entry);
stream.Write(buffer, 0, size);
try
{
//如果读入缓冲区中的字节数没有所请求的总字节数那么多
while (size < fs.Length)
{
int sizeRead = fs.Read(buffer, 0, buffer.Length);
stream.Write(buffer, 0, sizeRead);
size += sizeRead;
}
}
catch (Exception ex) { throw ex; }
finally { fs.Close(); }
}
//删除文件
File.Delete(fileNames[i]);
}
stream.Finish();
stream.Close();
}
下面来写导出Txt的方法
在此之前,先定义一个Salary类
public class Salary
{
/// <summary>
/// 员工编号
/// </summary>
public string EmpNo { get; set; }
/// <summary>
/// 员工姓名
/// </summary>
public string EmpName { get; set; }
/// <summary>
/// 部门
/// </summary>
public string Dept { get; set; }
/// <summary>
/// 职务
/// </summary>
public string Post { get; set; }
/// <summary>
/// 工资
/// </summary>
public decimal Salary { get; set; }
/// <summary>
/// 开户行
/// </summary>
public string Bank { get; set; }
/// <summary>
/// 银行账号
/// </summary>
public string BankNo { get; set; }
}
再写一个生成单个Txt文档字符串的方法
public static string GenTxtString(List<Salary> list)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Count; i++)
{
sb.Append(i + 1).Append(",");
sb.Append(list[i].EmpNo).Append(",");
sb.Append(list[i].EmpName).Append(",");
sb.Append(list[i].Dept).Append(",");
sb.Append(list[i].Post).Append(",");
sb.Append(list[i].Salary).Append(",");
sb.Append(list[i].Bank).Append(",");
sb.Append(list[i].BankNo).Append(",");
sb.AppendLine();
}
return sb.ToString();
}
导出Txt的方法
public static void ToTxt(DataSet ds, string fileName)
{
DataTable table = ds.Tables[0];
DataRowCollection rows = table.Rows;
DataColumnCollection columns = table.Columns;
//行数
int rowCount = rows.Count;
//列数
int columnCount = columns.Count;
//金额总和
decimal totalSalary = 0;
//Txt文档路径列表
List<string> fileList = new List<string>();
//Salary列表
List<Salary> salaryList = new List<Salary>();
//遍历所有行
for (int i = 0; i < rows.Count; i++)
{
//工资
decimal salary = Convert.ToDecimal(rows[i]["Salary"]);
//固定值5万
decimal c = 50000.00m;
//工资由几个5万组成
int a = (int)(salary / c);
//工资小于等于10万
if (salary <= 100000)
{
totalSalary += salary;
if (totalSalary > 100000)
{
//如果总额大于10万,新创建一个Txt
CreateTxtFile(GenTxtString(salaryList), fileList);
totalSalary = salary;
salaryList.Clear();
}
if (a > 0)
{
for (int j = 0; j < a; j++)
{
salaryList.Add(new Salary() { EmpNo = rows[i]["EmpNo"].ToString(), EmpName = rows[i]["EmpName"].ToString(), Dept = rows[i]["Dept"].ToString(), Post = rows[i]["Post"].ToString(), FactSalary = c, Bank = rows[i]["Bank"].ToString(), BankNo = rows[i]["BankNo"].ToString() });
}
//余数不为0
if (salary % c != 0)
{
salaryList.Add(new Salary() { EmpNo = rows[i]["EmpNo"].ToString(), EmpName = rows[i]["EmpName"].ToString(), Dept = rows[i]["Dept"].ToString(), Post = rows[i]["Post"].ToString(), FactSalary = salary % c, Bank = rows[i]["Bank"].ToString(), BankNo = rows[i]["BankNo"].ToString() });
}
}
else
{
salaryList.Add(new Salary() { EmpNo = rows[i]["EmpNo"].ToString(), EmpName = rows[i]["EmpName"].ToString(), Dept = rows[i]["Dept"].ToString(), Post = rows[i]["Post"].ToString(), FactSalary = salary, Bank = rows[i]["Bank"].ToString(), BankNo = rows[i]["BankNo"].ToString() });
}
}
//工资大于10万
else
{
for (int j = 0; j < a; j++)
{
totalSalary += c;
if (totalSalary > 100000)
{
CreateTxtFile(GenTxtString(salaryList), fileList);
totalSalary = c;
salaryList.Clear();
}
salaryList.Add(new Salary() { EmpNo = rows[i]["EmpNo"].ToString(), EmpName = rows[i]["EmpName"].ToString(), Dept = rows[i]["Dept"].ToString(), Post = rows[i]["Post"].ToString(), FactSalary = c, Bank = rows[i]["Bank"].ToString(), BankNo = rows[i]["BankNo"].ToString() });
}
if (salary % c != 0)
{
totalSalary += salary % c;
if (totalSalary > 100000)
{
CreateTxtFile(GenTxtString(salaryList), fileList);
totalSalary = salary % c;
salaryList.Clear();
}
salaryList.Add(new Salary() { EmpNo = rows[i]["EmpNo"].ToString(), EmpName = rows[i]["EmpName"].ToString(), Dept = rows[i]["Dept"].ToString(), Post = rows[i]["Post"].ToString(), FactSalary = salary % c, Bank = rows[i]["Bank"].ToString(), BankNo = rows[i]["BankNo"].ToString() });
}
}
}
if (totalSalary <= 100000)
{
CreateTxtFile(GenTxtString(salaryList), fileList);
}
CompressFile(@"F:\Txt\" + fileName + ".zip", fileName, fileList);
}
用以下数据构造DataSet。
| EmpNo | EmpName | Dept | Post | Salary | Bank | BankNo |
| 1000 | 刘德华 | 办公室 | 总经理 | 110000 | 交通银行 | 55784756584762 |
| 1001 | 周星驰 | 办公室 | 副总 | 105000 | 建设银行 | 58476594865764 |
| 1002 | 王力宏 | 财务 | 经理 | 90000 | 农业银行 | 95486437546534 |
| 1003 | 谢霆锋 | 财务 | 出纳 | 6000 | 工商银行 | 59583457547336 |
| 1004 | 吴彦祖 | 软件部 | 程序员 | 10300 | 建设银行 | 59445473365484 |
| 1005 | 林志颖 | 软件部 | 工程师 | 24000 | 中国银行 | 95584744956852 |
| 1006 | 何润东 | 软件部 | 工程师 | 30000 | 农业银行 | 95483574775483 |
| 1007 | 古天乐 | 软件部 | 实习生 | 3000 | 农业银行 | 95483474454499 |
| 1008 | 郑伊健 | 软件部 | 实习生 | 5000 | 农业银行 | 43945748374934 |
以上数据是我随便填的,可以使用任意数据替换。
在Main方法中调用ToTxt方法。
Txt.ToTxt(ds, "导出Txt测试");
Console.WriteLine("导出成功");
运行后,在F:\Txt下会生成压缩文件
双击“导出Txt测试.zip”,会看到生成的Txt文件,如下图所示:
这样生成的每个Txt里都不会超过10万。有兴趣的朋友可以帮我测试测试,看有没有什么地方不对!
下面我附上源码:下载
运行程序之前先在F盘下新建Txt文件夹,也可以修改程序里的Path。
来源:https://www.cnblogs.com/nianming/archive/2012/01/06/2315322.html

