我正在使用此代码
private void Form1_Load(object sender, EventArgs e)
{
System.Data.DataTable dt;
SqlConnection con=new SqlConnection("Data Source=sanjay;Initial Catalog=Login;Integrated Security=True");
SqlCommand cmd=new SqlCommand("select * from login",con);
SqlDataAdapter sda=new SqlDataAdapter(cmd);
dt=new System.Data.DataTable();
sda.Fill(dt);
int srno = 1;
StringBuilder stb = new StringBuilder();
ClosedXML.Excel.XLWorkbook wb = new ClosedXML.Excel.XLWorkbook();
String Todaysdate1 = DateTime.Now.ToString("dd-MM-yyyy");
ClosedXML.Excel.IXLWorksheet wt;
string appPath = Path.Combine(Environment.CurrentDirectory + "\\");
if (!Directory.Exists(appPath + "\\" + Todaysdate1 + "\\TD"))
{
Directory.CreateDirectory(appPath + "\\" + Todaysdate1);
}
string fileName = appPath + "\\" + Todaysdate1 + "\\TD_Results_From.XLS";
if (dt.Rows.Count > 0)
{
stb.Append("<html>");
stb.Append("<head>");
stb.Append("</head>");
stb.Append("<body>");
stb.Append("Sanju");
stb.Append("<table border=2>");
stb.Append("<tbody>");
stb.Append("<tr style='height:25px'>");
stb.Append("<td colspan='18' style='text-align:center;font-size:18px;font-weight:bold;min-width:50px'>");
stb.Append("<center> Form III - DEAF Voucher Preparation</center>");
stb.Append("</td>");
stb.Append("</tr>");
stb.Append("<tr style='height:21px'>");
stb.Append("<td colspan='18' style='text-align:center;font-size:16px;font-weight:bold;min-width:50px'>");
stb.Append("<center>2018-05-01</center>");
stb.Append("</td>");
stb.Append("</tr>");
stb.Append("<tr style='height:100px;font-weight:bold'>");
stb.Append("<td style='text-align:center;font-size:14px'>No.</td>");
stb.Append("</tr>");
for (int i = 0; i < dt.Rows.Count; i++)
{
stb.Append("<tr>");
stb.Append("<td>" + srno++ + " </td>");
stb.Append("<td nowrap>" + dt.Rows[i][0].ToString() + "</td>");
stb.Append("<td style='background-color:#ff99cc'></td>");
stb.Append("<td style='background-color:#ff99cc'></td>");
stb.Append("<td style='background-color:#ff99cc'></td>");
stb.Append("</tr>");
}
for (int i = 0; i < dt.Rows.Count; i++)
{
stb.Append("<table border=2>");
stb.Append("<tbody>");
stb.Append("<tr style='height:25px'>");
stb.Append("<td colspan='18' style='text-align:center;font-size:18px;font-weight:bold;min-width:50px'>");
stb.Append("<center> Form III - DEAF Voucher Preparation</center>");
stb.Append("</td>");
stb.Append("</tr>");
stb.Append("<tr style='height:21px'>");
stb.Append("<td colspan='18' style='text-align:center;font-size:16px;font-weight:bold;min-width:50px'>");
stb.Append("<center>2018-05-01</center>");
stb.Append("</td>");
stb.Append("</tr>");
stb.Append("<tr style='height:100px;font-weight:bold'>");
stb.Append("<td style='text-align:center;font-size:14px'>No.</td>");
stb.Append("</tr>");
stb.Append("<tr>");
stb.Append("<td>" + srno++ + " </td>");
stb.Append("<td nowrap>" + dt.Rows[i][0].ToString() + "</td>");
stb.Append("<td style='background-color:#ff99cc'></td>");
stb.Append("<td style='background-color:#ff99cc'></td>");
stb.Append("<td style='background-color:#ff99cc'></td>");
stb.Append("</tr>"); stb.Append("</tbody>"); stb.Append("</table>");
InsertWorksheet(i.ToString());
}
stb.Append("</tbody>");
stb.Append("</table>");
stb.Append("</body>");
stb.Append("</html>");
using (Stream stream = new FileStream(fileName, FileMode.Create))
{
wb.SaveAs(stream);
}
System.Diagnostics.Process.Start(appPath + "\\" + Todaysdate1 + "\\TD_Results_From.XLS");
}
}
public static void InsertWorksheet(string docName)
{
// Open the document for editing.
using(SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))
{
// Add a blank WorksheetPart.
WorksheetPart newWorksheetPart = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
newWorksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(new SheetData());
DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();
string relationshipId = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart);
// Get a unique ID for the new worksheet.
uint sheetId = 1;
if (sheets.Elements<Sheet>().Count() > 0)
{
sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
}
// Give the new worksheet a name.
string sheetName = "Sheet" + sheetId;
// Append the new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
sheets.Append(sheet);
}
}
我正在使用Html格式创建excel文件,并打开excel和数据视图,但是这样会发生错误
错误2类型'System.IO.Packaging.Package'在未引用的程序集中定义。您必须添加对程序集'WindowsBase,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'的引用。 C:\ Users \ test \ Documents \ Visual Studio 2010 \ Projects \多个excelsheet \多个excelsheet \ Form1.cs 143 13多个excelsheet