C#读取和导出EXCEL类,
可操作多个SHEET.无须安装EXCEL.
改进的一个EXCEL类,可以读取EXCEL,也可以将内容导出到EXCEL.操作多个SHEET比较方便.
而且无须安装任何EXCEL.
1.读取.
而且无须安装任何EXCEL.
1.读取.
读取
public void Read(string FileName)
{
AppLibrary.ReadExcel.Workbook workbook = null;
workbook = AppLibrary.ReadExcel.Workbook.getWorkbook(FileName);
int k = 0;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (AppLibrary.ReadExcel.Sheet sheet in workbook.Sheets)
{
k++;
sb.Append("当前为第" + k + "个Sheet<br/><br/>");
sb.Append("<table class='scroll' cellpadding=0 cellspacing=0>");
for (int iRow = 0; iRow < sheet.Rows; iRow++)
{
sb.Append("<tr>");
sb.Append("<td>");
sb.Append("当前为第" + (iRow + 1) + "行");
sb.Append("</td>");
for (int iCol = 0; iCol < sheet.Columns; iCol++)
{
AppLibrary.ReadExcel.Cell cell = sheet.getCell(iCol, iRow);
object val = cell.Value;
string cellValue = (val != null) ? val.ToString() : "";
sb.Append("<td>");
sb.Append(cellValue);
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("</table><br/><br/>");
}
Response.Write(sb.ToString());
}
public void Read(string FileName)
{
AppLibrary.ReadExcel.Workbook workbook = null;
workbook = AppLibrary.ReadExcel.Workbook.getWorkbook(FileName);
int k = 0;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (AppLibrary.ReadExcel.Sheet sheet in workbook.Sheets)
{
k++;
sb.Append("当前为第" + k + "个Sheet<br/><br/>");
sb.Append("<table class='scroll' cellpadding=0 cellspacing=0>");
for (int iRow = 0; iRow < sheet.Rows; iRow++)
{
sb.Append("<tr>");
sb.Append("<td>");
sb.Append("当前为第" + (iRow + 1) + "行");
sb.Append("</td>");
for (int iCol = 0; iCol < sheet.Columns; iCol++)
{
AppLibrary.ReadExcel.Cell cell = sheet.getCell(iCol, iRow);
object val = cell.Value;
string cellValue = (val != null) ? val.ToString() : "";
sb.Append("<td>");
sb.Append(cellValue);
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("</table><br/><br/>");
}
Response.Write(sb.ToString());
}
读取的EXCEL:
读取后:
导出:
导出
public void ToExcel()
{
AppLibrary.WriteExcel.XlsDocument doc = new AppLibrary.WriteExcel.XlsDocument();
doc.FileName = "Report.xls";
string SheetName = string.Empty;
//记录条数
int mCount = 55;
//每个SHEET的数量
int inv = 10;
//计算当前多少个SHEET
int k = Convert.ToInt32(Math.Round(Convert.ToDouble(mCount / inv))) + 1;
for (int i = 0; i < k; i++)
{
SheetName = "当前是SHEET" + i.ToString();
AppLibrary.WriteExcel.Worksheet sheet = doc.Workbook.Worksheets.Add(SheetName);
AppLibrary.WriteExcel.Cells cells = sheet.Cells;
//第一行表头
cells.Add(1, 1, "序号");
cells.Add(1, 2, "准考证号");
cells.Add(1, 3, "考生姓名");
cells.Add(1, 4, "性别");
cells.Add(1, 5, "报考专业");
int f = 1;
for (int m = i * inv; m < mCount && m < (i + 1) * inv; m++)
{
f++;
cells.Add(f, 1, f - 1);
cells.Add(f, 2, f);
cells.Add(f, 3, "woody.wu");
cells.Add(f, 4, "男");
cells.Add(f, 5, "经济学");
}
}
doc.Send();
Response.Flush();
Response.End();
}
public void ToExcel()
{
AppLibrary.WriteExcel.XlsDocument doc = new AppLibrary.WriteExcel.XlsDocument();
doc.FileName = "Report.xls";
string SheetName = string.Empty;
//记录条数
int mCount = 55;
//每个SHEET的数量
int inv = 10;
//计算当前多少个SHEET
int k = Convert.ToInt32(Math.Round(Convert.ToDouble(mCount / inv))) + 1;
for (int i = 0; i < k; i++)
{
SheetName = "当前是SHEET" + i.ToString();
AppLibrary.WriteExcel.Worksheet sheet = doc.Workbook.Worksheets.Add(SheetName);
AppLibrary.WriteExcel.Cells cells = sheet.Cells;
//第一行表头
cells.Add(1, 1, "序号");
cells.Add(1, 2, "准考证号");
cells.Add(1, 3, "考生姓名");
cells.Add(1, 4, "性别");
cells.Add(1, 5, "报考专业");
int f = 1;
for (int m = i * inv; m < mCount && m < (i + 1) * inv; m++)
{
f++;
cells.Add(f, 1, f - 1);
cells.Add(f, 2, f);
cells.Add(f, 3, "woody.wu");
cells.Add(f, 4, "男");
cells.Add(f, 5, "经济学");
}
}
doc.Send();
Response.Flush();
Response.End();
}