//IWorkbook wk = new HSSFWorkbook(); //写03的xls IWorkbook wk = new XSSFWorkbook(); //写07之后xlsx ISheet sheet = wk.CreateSheet("A1"); IRow row1 = sheet.CreateRow(0); //创建第一行 for (int i = 0; i < 10; i++) { //循环创建第一行的单元格并赋值 row1.CreateCell(i).SetCellValue("create"+i); } IRow row2 = sheet.CreateRow(1); //创建第二行 for (int i = 0; i < 8; i++) { row2.CreateCell(i).SetCellValue("delete" + i); } using (FileStream fs=File.OpenWrite("1213.xlsx")) { wk.Write(fs); MessageBox.Show("ok"); }
用npoi组件 很方便
npoi组件是什么,需要在窗体中增加其他控件吗,我现在的想法是菜单栏中有导出数据按钮,然后点击按钮,就把List<Point>中的数据导入到excel中了,excel中有三个列就行,分别是点的序号,点的X坐标值,点的Y坐标值,现在还缺少相应的using指令或程序集引用,求指点?
@中华神: 不好意思 你去这里下载npoi的程序集吧 里面有详细的教程和demo http://npoi.codeplex.com/
@中华神: @中华神: 不好意思啊 不需要增加控件 在原件的事件中 引入dll 你先去这里下载npoi的程序集吧 里面有详细的教程和demo http://npoi.codeplex.com/
@冰壶秋月:
@冰壶秋月: 想实现将Excel中的数据导入到数组中,然后根据点的坐标进行画图,但功能不能实现,下面是代码,代码有问题吗
using (FileStream fs = File.OpenRead("坐标.xls"))
{
int[] XZB = new int[list.Count / 10];
int[] YZB = new int[list.Count / 10];
IWorkbook wk = new HSSFWorkbook(); //读取07之xlsx
for (int i = 0; i < wk.NumberOfSheets; i++)
{
ISheet sheet = wk.GetSheetAt(i);
for (int j = 1; j <= sheet.LastRowNum; j++)
{
IRow row = sheet.GetRow(j);
XZB[j] = Convert.ToInt32(row.GetCell(1));//把Excel中的数据放到数组中
YZB[j] = Convert.ToInt32(row.GetCell(2));
}
}
for (int j = 1; j < list.Count / 10-1; j++)
{
Graphics graphic = CreateGraphics();
Pen p = new Pen(Color.Blue, 1);
graphic.DrawLine(p, XZB[j], YZB[j], XZB[j + 1], YZB[j + 1]);
}
@冰壶秋月: 不是生成图片,就是把Excel中的坐标存入数组中,然后用Graphics函数,用数组中的数据画线,但是不能显示
跟平时导出EXCEL一样啊
本人菜鸟,不会导出EXCEL,希望能给出代码,说的详细一些,谢谢
public static void Export(List<AttendanceItem> attendanceitemlist, AttendanceList attendancelist, AttendanceCriteria Criteria, FileInfo path, ref string Error) { try { Type type = typeof(Attendance); using (ExcelPackage package = new ExcelPackage(path)) { List<Attendance> attendanceidlist = new List<Attendance>(); ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("薪资导出数据-薪资导入模板"); if (attendancelist.Count != 0) { int index = 1; for (int j = 2; j < attendancelist.Count + 2; j++) //j代表 导出 的 行号 { worksheet.Cells[1, 1].Value = "工号";//一行一列 worksheet.Cells[1, 2].Value = "姓名";//一行二列 worksheet.Cells[1, 3].Value = "年份";//一行三列 worksheet.Cells[1, 4].Value = "月份";//一行四列 index++; attendanceidlist.Clear(); attendanceidlist.Add(attendancelist[j - 2]); worksheet.Cells[index, 1].Value = type.GetProperty("WorkId").GetValue(attendanceidlist[0], null); worksheet.Cells[index, 2].Value = type.GetProperty("EmployeeName").GetValue(attendanceidlist[0], null); worksheet.Cells[index, 3].Value = type.GetProperty("Year").GetValue(attendanceidlist[0], null); worksheet.Cells[index, 4].Value = type.GetProperty("Month").GetValue(attendanceidlist[0], null); for (int i = 1; i < attendanceitemlist.Count + 1; i++)//i代表列 { worksheet.Cells[1, i + 4].Value = attendanceitemlist[i - 1].CustomName; worksheet.Cells[index, i + 4].Value = float.Parse(type.GetProperty(attendanceitemlist[i - 1].AttendanceCode).GetValue(attendanceidlist[0], null).ToString()); using (var range = worksheet.Cells[1, 1, 1, attendanceitemlist.Count + 4]) { range.Style.Font.Bold = true; range.Style.Fill.PatternType = ExcelFillStyle.Solid; range.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.DarkBlue); range.Style.Font.Color.SetColor(System.Drawing.Color.White); } worksheet.HeaderFooter.OddHeader.CenteredText = "&24&U&\"Arial,Regular Bold\"薪资导出数据-薪资导入模板"; worksheet.HeaderFooter.OddFooter.CenteredText = ExcelHeaderFooter.SheetName; } } } package.Save(); } } catch { Error = "可能存在空数据,导出出错"; return; } }
将List的数据写入Datatable,然后直接Update即可。数据量不大可用。
怎样将listpoint的数据写入Datatable,能给个代码的例子吗?
@中华神:
var dt = new DataTable(); dt.Columns.Add("CDate", typeof(DateTime)); dt.Columns.Add("Type", typeof(string)); foreach (var day in list) { var row = dt.NewRow(); row["CDate"] = day.CDate; row["Type"] = day.Type; dt.Rows.Add(row); }
@幻天芒: 这是javascript的代码吧
@中华神:C#代码!