标签:stream save this dwr bre update file public for
public void Daochu()
{
DataTable table = bll.GetToILLModelList();
//创建一个工作蒲
HSSFWorkbook work = new HSSFWorkbook();
//创建一个Sheet
HSSFSheet sheet = work.CreateSheet();
//添加第一行数据
HSSFRow row = sheet.CreateRow(0);
//去循环添加第一行的列属性
for (int i = 0; i < table.Columns.Count; i++)
{
//设置列的标题
HSSFCell cell = row.CreateCell(table.Columns[i].Ordinal);
cell.SetCellValue(table.Columns[i].ColumnName);
}
//循环遍历去添加行数据
for (int i = 0; i < table.Rows.Count; i++)
{
//实例化一行
HSSFRow rows = sheet.CreateRow(i + 1);
for (int j = 0; j < table.Columns.Count; j++)
{
//实例化一列
HSSFCell cell = rows.CreateCell(table.Columns[j].Ordinal);
//指定第几行第几列去添加数据
cell.SetCellValue(table.Rows[i][table.Columns[j].Ordinal].ToString());
}
}
this.saveFileDialog1.Filter = "全部格式|*.xls";
DialogResult result = this.saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
using (FileStream fs = new FileStream(this.saveFileDialog1.FileName, FileMode.Create, FileAccess.ReadWrite))
{
//先去写文件
work.Write(fs);
//设置一个字节大小为4096
int size = 4096;
byte[] data = new byte[4 * 1024];
while (true)
{
if (size > 0)
{
size = fs.Read(data, 0, data.Length);
fs.Write(data, 0, size);
}
else
{
break;
}
}
}
}
//压缩文件
using (ZipFile zip = ZipFile.Create(this.saveFileDialog1.FileName.Split(‘.‘)[0].ToString() + ".rar"))
{
zip.BeginUpdate();
zip.Add(this.saveFileDialog1.FileName, new FileInfo(this.saveFileDialog1.FileName).Name);
zip.CommitUpdate();
};
}
标签:stream save this dwr bre update file public for
原文地址:https://www.cnblogs.com/GaoHao518/p/13065206.html